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

标签 asp.net c#-4.0

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/

相关文章:

asp.net - 防止 Web 表单错误重定向

c# - 有没有办法在不选择 'Allow less secure apps' 选项的情况下使用 ASP.NET 通过 Google Apps 帐户发送电子邮件?

c# - Sys.WebForms.PageRequestManagerParserErrorException : The message received from the server could not be parsed

encryption - 可以锁定目录,因此只有单个应用程序域可以访问它

c# - ASP.Net 应用程序作为 IIS_USR 执行 powershell 脚本

asp.net - 在运行时设置RegularExpressionValidator ValidationExpression

excel - 在此之前确定VSTO 4中的Office版本。应用程序已设置

linq - 向我推荐合适的 LINQ 提供程序(SQL 服务器、复杂查询)

jquery - 从 jQuery 调用 ASP.NET 4.0 WCF 服务会产生 400 Bad Request

c# - c#中的可选参数