日期:2014-05-18  浏览次数:20412 次

GridView 行主键取值;GridView 行主键取值;GridView 行主键取值;GridView 行主键取值;
GridView1   前台绑定表   TB1,   并加一个Button列,怎么能单击Button   取得该行的   主键值   ??

感觉不难,可怎么就是取不出来呢?郁闷了。。
在线等,。。。。。。。。

------解决方案--------------------
绑定列换成模板列
<asp:TemplateField HeaderText= "功能选择 ">
<HeaderStyle HorizontalAlign= "left "/>
<ItemStyle HorizontalAlign= "left "/>
<ItemTemplate>
<button id= "id " commandname= "id " commandagrument= ' <%#Bind( "ID ") %> '>
</ItemTemplate>
</asp:TemplateField>
为gridview添加rowcommand事件,
在事件内
if(e.commandname==(button commandname))
{
string id=e.commandagrument.tostring();
string strTxt= "delete tab1 where id= ' "+id+ " ' ";
//执行删除代码
}

------解决方案--------------------
DataKeyNames 属性输入主键字段名
点击删除按钮时
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Response.Write(GridView1.DataKeys[e.RowIndex].Value);
}