1

我创建了一个 Web 应用程序,其中我使用了 onrowcommand 并在后面的代码中创建了处理程序,现在我在空模板中有一个按钮,每当我单击该按钮时,我的 onRowCommand 都不会执行。下面是我的代码。

<asp:GridView ID="grdExternalLinkSection1" runat="server" Width="100%" AutoGenerateColumns="false" CellPadding="5" OnRowCommand="grdExternalLinkSection_RowCommand">
                                <EmptyDataTemplate>
                                    External Link Title
                                    <asp:TextBox ID="txtExternalLinkTitleEmptySection1" runat="server"></asp:TextBox>
                                    External Link Url
                                    <asp:TextBox ID="txtExternalLinkUrlEmptySection1" runat="server"></asp:TextBox>
                                    <asp:Button ID="btnExternalLinkEmptySection1" runat="server" Text="Add" CommandArgument="1" CommandName="headernew" style="padding:3px; width:56px;" />
                                </EmptyDataTemplate>
</asp:GridView>

还有更多的领域,但这就是我要说的。这是我的 RowCommand 事件处理程序背后的代码。

protected void grdExternalLinkSection_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Response.Write("welcome");
    }

它从不执行处理程序,下面是我的页面指令:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="NewsletterASPVersion.ascx.cs" Inherits="RWO_Controls_NewsletterASPVersion" %>

这工作了一次,之后就再也没有工作了。有谁知道是什么原因造成的。

4

1 回答 1

4

您可能陷入了两个可能的陷阱:

  1. 您在每次回发时将 GridView 重新绑定到它的 DataSource。所以总是检查:

    if(!IsPostBack)BindGrid();
    
  2. grdExternalLinkSection1.DataBind()当 DataSource 为空时,您没有调用

但是你根本看不到EmptyDataTemplate。所以我猜你已经掉进了第一个陷阱。

于 2012-03-07T16:14:39.753 回答