如何用GridView实现点击某一行的超链接就可以获得该行的ID?
我现在做了一个GridView的列表,这个列表有两列,一列为模板列(是LinkButton的),另一列显示的ID,我想实现如下功能:
当我点击某一行的超链接时,就可以获得这一行的第二列的相应ID?
如何实现此功能?小弟先谢谢了!
我之前实现同样功能是用DataList中的OnItemCommand事件,但是在DataList里面又没有像GridView里的RowCreated事件那样在创建行的同时给其添加鼠标事件让鼠标所在行高亮显示。我想用一个DATA控件同时实现这两个功能。
------解决方案--------------------前台加个模板列隐藏起来绑定ID
<asp:TemplateField Visible= "False ">
<ItemTemplate>
<asp:CheckBox ID= "CheckBox1 " runat= "server " />
<asp:Label ID= "MyName " runat= "server " Text= ' <%#Eval( "ID字段 ") %> '> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType= "Button " CausesValidation= "false " HeaderText= "编辑列 " Text= "编辑 "
CommandName= "Select ">
<ItemStyle HorizontalAlign= "Center " />
</asp:ButtonField>
<asp:ButtonField ButtonType= "Button " CausesValidation= "false " HeaderText= "删除列 " Text= "删除 "
CommandName= "Delete ">
<ItemStyle HorizontalAlign= "Center " />
</asp:ButtonField>
我做的时候没有用模板列里放linkButton,我直接用buttonField,
后台直接在RowCommand事件里取
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select ")
{
int index = Convert.ToInt32(e.CommandArgument);
Label lbl = (Label)GridView1.Rows[index].FindControl( "MyName ");
ShowData(int.Parse(lbl.Text));
}
else if (e.CommandName == "Delete ")
{
int index = Convert.ToInt32(e.CommandArgument);
Label lbl = (Label)GridView1.Rows[index].FindControl( "MyName ");
col.Delete(int.Parse(lbl.Text));
PageLoad();
}
}