被常常问到的一个经典问题了,答案放到网上来。
步骤1 ASP.NET的这个地方这样设置。GridView加入 OnRowCommand 事件,模版列的按钮设置CommandArgument、CommandName
<
asp:GridView
ID
="GridView1"
runat
="server"
CellPadding
="4"
AutoGenerateColumns
="False"
OnRowCommand
="GridView1_RowCommand"
>
< Columns >
< asp:BoundField DataField ="id" HeaderText ="编号" />
< asp:BoundField DataField ="loginID" HeaderText ="登陆名" />
< asp:TemplateField HeaderText ="工具栏权限管理" >
< ItemTemplate >
< asp:Label ID ="Lblqx1" runat ="server" ></ asp:Label >
< asp:Button ID ="BtnGjl" runat ="server" Text ="设置" CommandArgument ='<%#Eval("id") % > ' CommandName="gjl" />
</ ItemTemplate >
</ asp:TemplateField >
</ Columns >
</ asp:GridView >
< Columns >
< asp:BoundField DataField ="id" HeaderText ="编号" />
< asp:BoundField DataField ="loginID" HeaderText ="登陆名" />
< asp:TemplateField HeaderText ="工具栏权限管理" >
< ItemTemplate >
< asp:Label ID ="Lblqx1" runat ="server" ></ asp:Label >
< asp:Button ID ="BtnGjl" runat ="server" Text ="设置" CommandArgument ='<%#Eval("id") % > ' CommandName="gjl" />
</ ItemTemplate >
</ asp:TemplateField >
</ Columns >
</ asp:GridView >
2 后台cs的GridView1_RowCommand方法
protected
void
GridView1_RowCommand(
object
sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "gjl")
{
Response.Redirect("NewPage.aspx?key=" + e.CommandArgument.ToString(), false);
}
}
{
if (e.CommandName == "gjl")
{
Response.Redirect("NewPage.aspx?key=" + e.CommandArgument.ToString(), false);
}
}