gridview模板列按钮的使用及获取本行主键值

来源:互联网 发布:日系女鞋品牌知乎 编辑:程序博客网 时间:2024/06/05 09:02

很多人在使用asp.net 2.0的gridview中的模板列时不知道如何获取当前行号,datagrid中是可以直接获得的.

在gridview中,事件的参数变成了GridViewCommandEnentArgs,我们可以数据绑定后给相应模板列控件指定事件参数,在处理其事件的时候就再获取出来

使用模板列时控件CommandName不要取与删除列和编辑、更新列相同的名字,如delete,否则会引发相应的事件

一个简单的事例如下:

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName=="UpdateStatus")
        {
            string docid = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[0].Text;
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btUpSts= (Button)e.Row.FindControl("bt_upsts");
            btUpSts.CommandArgument = e.Row.RowIndex.ToString();
        }
    }