2012-01-30 44 views
0

我使用ButtonField和几个BoundFields动态创建GridView。 ButtonField按钮类型是LinkBut​​ton。如果我运行它,buttonclick会触发一个回发,但rowCommand不会被触发。即使我使用AutogenerateSelectButton,它也不会被触发。这个事件是通过计算机绑定的。代码如下:RowCommand未被触发动态创建的GridView

protected void B_Search_Click(object sender, EventArgs e) //Search buttonclick that creates and displays the gridview 
    { 
     gd = getGridView(); //defines the gridview with columns and buttonfield 

     gd.DataSource = executeAdvanceSearch(); //retrieves data from DB as Dataset 
     gd.DataBind(); 

     gd.RowCommand += new GridViewCommandEventHandler(gdView_RowCommand); //Rowcommand event binding 
     PlaceHolder1.Controls.Add(gd); 
    } 

    protected void gdView_RowCommand(object sender, GridViewCommandEventArgs e) //not getting triggered on postback 
    { 
     int index = Convert.ToInt32(e.CommandArgument); 

     GridView gdView = (GridView)sender; 

     if (e.CommandName == "IDClick") 
     { 
      //Do something 
     } 
    } 


    private GridView getGridView() 
    { 
     GridView gdView = new GridView(); 
     gdView.AutoGenerateColumns = false; 

     gdView.AutoGenerateSelectButton = true; 

     string name; 

     string[] field = ZGP.BLL.Search.getResultFormat(Convert.ToInt32(DDL_ResultView.SelectedValue), out name); //Ignore. This jst gets columnNames 

     if (field.Count() != 0) 
     { 
      gdView.Columns.Add(getSelectButton()); //Adds linkbutton 

      foreach (string cName in field) //ignore. This adds columns. 
       if (!String.IsNullOrEmpty(cName)) 
       { 
        gdView.Columns.Add(GV_DataColumn.getGridViewColumn((DataColumnName)Enum.Parse(typeof(DataColumnName), cName))); //Ignore. adds columns 
       } 
     } 

     return gdView; 
    } 

    private ButtonField getSelectButton() 
    { 
     ButtonField _bf = new ButtonField(); 
     _bf.ButtonType = ButtonType.Link; 
     _bf.HeaderText = "ID"; 
     _bf.DataTextField = "ID"; 
     _bf.CommandName = "IDClick"; 
     return _bf; 
    } 

感谢您的帮助。

回答

0

您需要在每个回发中创建网格视图。

+0

尝试在会话中存储gridview。并限制每个帖子后面的事件不起作用。感谢您的回复 – Gnani 2012-01-30 12:34:33

+0

谢谢你尝试不同的方式,它的工作 – Gnani 2012-01-30 12:36:29

1
if (e.CommandName=="CommandName") 
    { 
     int index = Convert.ToInt32(e.CommandArgument); 
     GridViewRow row = GridView1.Rows[index]; 
     string boundFieldText= row.Cells[0].Text; 
    }