gpt4 book ai didi

asp.net - 如何找到 GridView 按钮单击事件?

转载 作者:行者123 更新时间:2023-12-04 00:40:47 24 4
gpt4 key购买 nike

Button btnAddrecord = (Button)sender;
GridViewRow gvr =(GridViewRow)btnAddrecord.NamingContainer;
if (btnAddrecord.CommandName == "onAddMaterial")

最佳答案

在 GridView 标记中定义按钮并为按钮分配一个 CommandName 值,如下所示:

<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server"  
OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Add Record">
<ItemTemplate>
<asp:Button ID="btnAddRecord"
CommandArgument="<%# ((GridViewRow)Container).RowIndex %>"
CommandName="AddRecord" runat="server" Text="Add" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

现在在您的代码隐藏中,您可以处理 OnRowCommand 事件和 AddRecord 命令,如下所示:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddRecord")
{
// Get index of row passed as command argument
int index = Convert.ToInt32(e.CommandArgument.ToString());

// Your logic here
}
}

关于asp.net - 如何找到 GridView 按钮单击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18972987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com