GridView中的RowCommand事件中的取值问题GridView的RowCommand事件中获取该行DataKey值(转)

需求现象:在RowCommand事件中获取该行DataKey值,以便编辑改行相应的数据;

解决方案:
1)模板列中的LinkButton ,需要绑定其CommandArgument=’<%# Bind(“ID”) %>’,然后在RowCommand事件中获取
绑定:





’ runat=“server” >


获取:
string ID = e.CommandArgument.ToString();

2)非模板列的ButtonField,不需要绑定其CommandArgument,就可以获取
绑定:



获取:
int rowNumber = Convert.ToInt32(e.CommandArgument);
string ID = dg.DataKeys[rowNumber].Value.ToString();
先看前台代码





’>


’ Width=“50” >

             
                                
                                    
                                
                                
                                    
                                    
                                
                            
              
  要在 GridView1_RowCommand 方法中取TextBox9的值,无法直接取。

这个时候要用下面的办法

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string workid = e.CommandArgument.ToString();
string name = e.CommandName.ToString();
string strUpdate = “”;
string strRemark = “”;
string strEffective = “”;

    GridViewRow row = ((Control)e.CommandSource).BindingContainer as GridViewRow;
    //取有效时间
    TextBox txtEffectiveTime = (TextBox)row.FindControl("TextBox9");
    strEffective = (string.IsNullOrEmpty(txtEffectiveTime.Text) != true) ? txtEffectiveTime.Text.Trim() : "";
    //取审备注
    TextBox txtNotion = (TextBox)row.FindControl("TextBox12");
    strRemark = (string.IsNullOrEmpty(txtNotion.Text) != true) ? txtNotion.Text.Trim() : "";

}
  最有效的就是这段

GridViewRow row = ((Control)e.CommandSource).BindingContainer as GridViewRow;

//如果要去id为TextBox9的值

TextBox txtEffectiveTime = (TextBox)row.FindControl(“TextBox9”);

你可能感兴趣的:(GridView中的RowCommand事件中的取值问题GridView的RowCommand事件中获取该行DataKey值(转))