gridview模版列中加了一个button用来删除数据,那怎么获取所在列数据的id

来源:百度知道 编辑:UC知道 时间:2024/05/22 01:21:50
在button的onclick事件里吗
在GridView_RowDeleting这个事件里用应该不行的,因为gridvewi中显示的数据很复杂,来自多个表,所以我想在button的onclick事件中来删

不是在button的onclick事件里
这样,在GridView 加上个事件 OnRowDeleting 在DataKeyNames属性里写上你表中代表ID的字段。
如:
<asp:GridView ID="GridView" runat="server" DataKeyNames="ID" AllowPaging="true" AutoGenerateColumns="false" OnRowDeleting="GridView_RowDeleting">

------------
在后台CS里 这样写:
protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string ID = this.GridView.DataKeys[e.RowIndex].Value.ToString(); //这样ID就找到了

}

e.item.index

用itemcommand,并且可以设置datekey

设置button 的CommandName="delete" 在RowDeleting时间里处理就行了

在前台GridView放置一个Label 设置为不可见 将其text="你的主键"
在后台
for(int i=0;i<GridView1.Rows.count;i++)
{string ID=(Label)GridView1.Rows[i].FindControl("abel");
之后Response.Redirect("del.aspx?send_id="+ID+",false);
}
在del.aspx页进行删除 删除成功之后在跳转回来 这是好理解的一种