GridView RowCommand未触发

时间:2017-01-13 16:35:01

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

我无法弄清楚为什么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 Page     

<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的GridView会导致RowCommand正常启动。

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

如何在按钮点击之前隐藏div而不阻止RowCommand正常触发?

2 个答案:

答案 0 :(得分:5)

将其从Button更改为LinkButton即可。

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

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

enter image description here

答案 1 :(得分:1)

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

<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;

   }}