【问题标题】:Gridview linkbutton not firing RowCommandGridview 链接按钮不触发 RowCommand
【发布时间】:2015-07-09 19:43:26
【问题描述】:

我正在设计一个搜索页面,如果单击“查看/编辑”链接按钮,则会加载另一个页面,其中包含有关该主题的更多信息;单击按钮时,它不会引发 Gridview 的 RowCommand 事件。 Gridview 是根据搜索词动态构建的,并通过 SQLDataSource 与数据库进行比较。这是 Gridview 的 html。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="RID" DataSourceID="Report_System" CellPadding="4" ForeColor="#333333" GridLines="None" OnDataBound="GridView1_DataBound" Width="739px" OnRowCommand="GridView1_RowCommand">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="RID" HeaderText="Report ID" InsertVisible="False" ReadOnly="True" SortExpression="RID" />
            <asp:BoundField DataField="Rdate" HeaderText="Report Date" SortExpression="Rdate" />
            <asp:BoundField DataField="Rtype" HeaderText="Type" SortExpression="Rtype" />
            <asp:BoundField DataField="OID" HeaderText="Officer ID" SortExpression="OID" />
            <asp:BoundField DataField="Ofirstname" HeaderText="Officer" SortExpression="Ofirstname" />
            <asp:BoundField DataField="Pname" HeaderText="Student Reportee" SortExpression="Pname" />
            <asp:ButtonField CommandName="View/Edit" Text="View/Edit" />
        </Columns>
        <EditRowStyle BackColor="#7C6F57" />
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#E3EAEB" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F8FAFA" />
        <SortedAscendingHeaderStyle BackColor="#246B61" />
        <SortedDescendingCellStyle BackColor="#D4DFE1" />
        <SortedDescendingHeaderStyle BackColor="#15524A" />
    </asp:GridView>

目前,应该将 RowCommand 事件简单地发回标签以进行测试。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //get row index
        int row = Convert.ToInt32(e.CommandArgument);

        errorLabel.Text = row.ToString();
    }

我已确保 ViewState 已启用,OnRowCommand 位于正确的位置,并且我的 PageLoad 事件为空。除此之外,我找不到任何论坛帖子可以帮助我。有什么想法吗?

【问题讨论】:

  • 我曾多次与这个问题作斗争,但我已经太久无法记住我尝试过的所有事情以及为什么各种选项都不起作用。但是,我可以告诉你我现在做什么,这对我有用:添加 OnRowEditing 事件而不是 OnRowCommand。编辑:哦,让命令名“编辑”而不是“查看/编辑”。
  • 尝试将 转换为 TemplateField。
  • 更改事件不起作用,因此我尝试对每个事件进行测试,唯一触发的事件是 PreRender 事件,这很明显。当我按下按钮时没有触发任何东西,这告诉我还有其他问题。我还没有完全尝试过 TemplateField 的想法,但奇怪的是根本没有触发任何事件。
  • 那么你找到解决方案了吗?很高兴与社区分享。
  • 我在链接按钮上遇到了这个问题。添加 CausesValidation="false"

标签: c# asp.net gridview


【解决方案1】:

尝试使用if 语句在GridView1_RowCommand 中添加对CommandName 的检查,如下所示

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      if(e.CommandName == "View/Edit")
      {
        //get row index
        int row = Convert.ToInt32(e.CommandArgument);

        errorLabel.Text = row.ToString();
      }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多