前台添加一模版列,里面添加一个Button
<asp:TemplateField HeaderText="测试">
<ItemTemplate>
<asp:Button ID="Button1" CommandName="btn" runat="server" Style="position: relative" Text="Button" />
</ItemTemplate>
</asp:TemplateField>
后台
protected void gv_Company_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "btn")
{
int index = Convert.ToInt32(e.CommandArgument);
DataKey key = this.gv_Company.DataKeys[index];
string tt = key.Value.ToString();
Response.Write(tt);
}
}
//行数据绑定
protected void gv_Company_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button bt = new Button();
bt = (Button)e.Row.Cells[6].FindControl("Button1");
bt.CommandArgument = e.Row.RowIndex.ToString();
}
}
第二种方式:
int index = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)).RowIndex;
本文介绍如何在ASP.NET中使用GridView控件添加带有CommandName的Button,并在后台通过RowCommand事件获取点击按钮所在行的数据索引及DataKey值。
2470

被折叠的 条评论
为什么被折叠?



