前台代码:
<ItemTemplate> <asp:ImageButton ID="ibtnUpdate" runat="server" CommandArgument='<%# Eval("studentNum")%>'CommandName="edit" /> </ItemTemplate>就可以通过GridView的RowCommand事件获得此行的主键,进而获得此行数据。 protected void gvSelectTask_RowCommand(objectsender, GridViewCommandEventArgs e) { string temp =e.CommandArgument.ToString().Trim(); //string[] num = temp.Split(','); string tag =e.CommandName.ToString().Trim(); //if (tag == "edit") //{ //CreativeStudio.Common.alert.GoHref("selectTaskEdit.aspx?studentN="+ num[0] +"&teacherN="+num[1]+""); //} //else if (tag == "delete") //删除选课记录的时候没有必要删除学生记录 //{ //sTaskBLL.Delete(num[0]); //Bind(); //} } 2.如果涉及到主键是两个字段或者要同时绑定两张表的主键那么用法为 前台代码: <ItemTemplate> <asp:ImageButton ID="ibtnUpdate" runat="server" CommandArgument='<%# Eval("studentNum")+","+Eval("teacherNum")%>'CommandName="edit" /> </ItemTemplate>同样可以获得表中一行数据,或者同时获得两张表的各一行数据。 protected void gvSelectTask_RowCommand(objectsender, GridViewCommandEventArgs e) { string temp =e.CommandArgument.ToString().Trim(); string[] num = temp.Split(','); string tag =e.CommandName.ToString().Trim(); //if (tag == "edit") //{ //CreativeStudio.Common.alert.GoHref("selectTaskEdit.aspx?studentN="+ num[0] +"&teacherN="+num[1]+""); //} //else if (tag == "delete") //删除选课记录的时候没有必要删除学生记录 //{ //sTaskBLL.Delete(num[0]); //Bind(); //} }