【问题标题】:GridView RowCommand Not FiringGridView RowCommand 未触发
【发布时间】:2017-01-13 16:35:01
【问题描述】:

我无法弄清楚 RowCommand 不触发的原因。奇怪的是,我在页面上有一个不同的 GridView,它的 RowCommand 没有问题地触发,所以我不知道问题可能是什么。

我有以下代码:

JavaScript

function displayPayees() {
        $('#payeeList').css("display", "block");
        $('#payeeList').dialog({
            modal: true,
            draggable: false,
            resizable: false,
            maxHeight: 600,
            width: 800,
            position: { my: "center top", at: "center top+15%", of: window }
        }); 
    }

.aspx 页面

<div id="payeeList" title="Available Payees" style="display:none;">
    <asp:GridView ID="gvPayees" runat="server" Visible="true" AutoGenerateColumns="false" ShowHeaderWhenEmpty="true" Width="100%" AllowSorting="True" OnRowCommand="gvPayees_RowCommand" CssClass="table table-striped">
        <HeaderStyle Font-Bold="True" ForeColor="White" Height="15px" BackColor="#46596b" Wrap="False"></HeaderStyle>
        <Columns>
            <asp:BoundField DataField="TaxID" HeaderText="Tax ID"></asp:BoundField>
            <asp:BoundField DataField="Name" HeaderText="Name"></asp:BoundField>
            <asp:BoundField DataField="Address1" HeaderText="Address"></asp:BoundField>
            <asp:BoundField DataField="Address2" HeaderText="Address 2"></asp:BoundField>
            <asp:BoundField DataField="City" HeaderText="City"></asp:BoundField>
            <asp:BoundField DataField="State" HeaderText="State"></asp:BoundField>
            <asp:BoundField DataField="Zip" HeaderText="Zip"></asp:BoundField>
            <asp:TemplateField HeaderText="">
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                <ItemTemplate>
                    <asp:Button ID="btnSelectPayee" runat="server" Text="Select" CssClass="btn btn-default home-btn btn-sm" CausesValidation="false" CommandName="SelectPayee" CommandArgument='<%# Eval("TaxID") & "~" & Eval("Name") & "~" & Eval("Address1") & "~" & Eval("Address2") & "~" & Eval("City") & "~" & Eval("State") & "~" & Eval("Zip") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>

代码背后

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
            BindDetails()
        End If
End Sub

Protected Sub gvPayees_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    Throw New Exception("Hello?") 'Test code
End Sub

更新:

我注意到将 GridView 移出其中包含 div 会导致 RowCommand 正确触发。

我正在使用jQuery UI 将 GridView 放入一个对话框中,以便在单击按钮时显示。我在包含div 上有display: none,因此在单击按钮之前 GridView 是不可见的,我认为这可能是我的问题的根源。我正在这里更新我的代码以反映这一点。

如何在不阻止 RowCommand 正确触发的情况下隐藏 div 直到单击按钮?

【问题讨论】:

  • 您的页面中有EnableEventValidation="true" 吗?
  • @AfnanAhmad - 我的代码中根本没有明确设置EnableEventValidation。我已经尝试将其设置为truefalse,但结果没有变化。
  • 让我感到奇怪的是,我在该页面上有另一个 GridView 可以正常工作。据我所知,除了名称(当然)以及另一个 GridView 使用 &lt;asp:ImageButton&gt; 而不是常规按钮这一事实之外,所有代码都是相同的。
  • 我偶尔会遇到这种情况。一旦我用尽了一切,我删除了网格并重新制作了它。那解决了它。不知道为什么。
  • 另外,只是为了好玩,设置CommandArgument="test"。您的其中一项是否为空并不重要,但您永远不知道。

标签: c# asp.net vb.net gridview


【解决方案1】:

将其从 Button 更改为 LinkButton 即可。

<ItemTemplate>
    <asp:LinkButton ID="btnSelectPayee" runat="server" CommandName="SelectPayee" 
        CommandArgument='<%# Eval("TaxID") %>' >Select</asp:LinkButton>
</ItemTemplate>

显然,jQuery UI 对话框将模式置于&lt;/form&gt; 标记之外,因此普通按钮不再起作用。但是 LinkBut​​tons 使用 JavaScript 来执行 PostBack,它们仍然在框架内注册。

【讨论】:

    【解决方案2】:

    首先,您应该将命令名称分配给项目模板中的按钮,如下所示:

    <ItemTemplate>
        <asp:Button ID="Btn1" CssClass="SelectRow" CommandName="Sign" runat="server"
            CausesValidation="false"
            CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" 
            Text="" />
    </ItemTemplate>
    

    在后面的代码中

    protected void gvPayees_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Sign")
        {
            GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
    
            int RowIndex = gvr.RowIndex;
            gvPayees.SelectedIndex = RowIndex;    
        }
    }
    

    【讨论】:

    • 这并没有解决我的问题。 RowCommand 仍然没有触发。
    • 我也更新了这个问题。我发现从其父 div 中删除 GridView 会使 RowCommand 正确触发。我猜我的问题源于我隐藏 GridView 直到单击按钮。我只是不确定如何解决它。
    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多