你把这个字段绑定到GridView的DataKeysName上
button的CommandName 改为"自己定义"
在GridView的RowCommand事件里写
if(e.CommandName == "button的CommandName")
{
//点button要的功能代码
}
button的CommandName 改为"自己定义"
在GridView的RowCommand事件里写
if(e.CommandName == "button的CommandName")
{
//点button要的功能代码
}
1. 3楼的方法:把模板列中的Button的CommandName设为btn
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "btn")
{
GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).Parent).Parent;
Response.Write(gvr.Cells[7].Text);
}
}
2. 和上面类似,只是给模板列中的Button绑定了Click事件,没有设置CommandName
protected void Button1_Click(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(((Button)sender).Parent).Parent;
Response.Write(gvr.Cells[7].Text);
} 如果模板列中只有一个Button而没有其它控件的话,建议使用GridView自身的ButtonField,只需设定CommandName
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "btn")
{
//e.CommandArgument为当前行号-1
Response.Write(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[7].Text);
}
}