aspx页面
aspx.cs类文件
<asp:GridView runat="server" ID="grd" AutoGenerateColumns="false" DataKeyNames="leagueID" OnRowCommand="grd_RowCommand" OnRowCreated="grd_RowCreated">
<Columns>
<asp:TemplateField HeaderText="我要加入">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Button runat="server" ID="btnJoin" Text="加 入" CommandName="btnJoin" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<Columns>
<asp:TemplateField HeaderText="我要加入">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Button runat="server" ID="btnJoin" Text="加 入" CommandName="btnJoin" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
aspx.cs类文件
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "btnJoin")
{
int index = Data.GetInt(e.CommandArgument);
int iLeagueID = Data.GetInt(grd.DataKeys[index].Value);
Response.Write("<script>alert('" + iLeagueID + "')</script>");
}
}
protected void grd_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnJoin = (Button)e.Row.FindControl("btnJoin");
btnJoin.CommandArgument = e.Row.RowIndex.ToString();
}
}
{
if (e.CommandName == "btnJoin")
{
int index = Data.GetInt(e.CommandArgument);
int iLeagueID = Data.GetInt(grd.DataKeys[index].Value);
Response.Write("<script>alert('" + iLeagueID + "')</script>");
}
}
protected void grd_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnJoin = (Button)e.Row.FindControl("btnJoin");
btnJoin.CommandArgument = e.Row.RowIndex.ToString();
}
}