您的位置:首页 > 其它

GridView的模版列中加入按钮,触发按钮事件后,如何获取该行的某个值?

2012-08-10 21:35 405 查看
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</ItemTemplate>
</asp:TemplateField>
=============================================
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
GridViewRow row = btn.Parent.Parent as GridViewRow;
string a = row.Cells[0].ToString();//获得第一个单元格的值
string b = this.GridView1.DataKeys[row.DataItemIndex].Values[0];//获得DataKeys的值
}

或者

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandName="MyCommand" Text="Button" />
</ItemTemplate>
</asp:TemplateField>
===============================================
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "MyCommand")
{
Button btn = e.CommandSource as Button;
GridViewRow row = btn.Parent.Parent as GridViewRow;
string a = row.Cells[0].ToString();//获得第一个单元格的值
string b = this.GridView1.DataKeys[row.DataItemIndex].Values[0];//获得DataKeys的值
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐