删除列后未触发GridView RowCommand

时间:2018-10-18 20:11:04

标签: asp.net vb.net gridview webforms rowcommand

我有一个GridView,其中包含一些数据和几个LinkBut​​ton列。如果用户没有特定角色,则从GridView中删除LinkBut​​ton列之一。

我的问题是,当我删除该列时,RowCommand事件将不再触发,并且当我单击它们时,所有LinkBut​​ton都将消失。

注意::我没有在PostBack上重新绑定网格,没有禁用ViewState,并且我尝试设置CausesValidation="false"且未做任何更改。我在网上找到与此问题的每个人都遇到了其中一个问题。

如果我注释掉删除该列的代码,那么一切都会正常进行。

编辑:如果我隐藏该列而不是用gvMyGrid.Columns(0).Visible = False删除它,则一切正常。

为什么要删除该列以防止触发该事件?

这是我的GridView:

<asp:GridView ID="gvMyGrid" runat="server" Visible="true" EmptyDataText="Nothing to show." AutoGenerateColumns="false" ShowHeaderWhenEmpty="true" Width="100%" AllowSorting="True">
    <HeaderStyle Font-Bold="True" ForeColor="White" Height="15px" BackColor="#46596b" Wrap="False"></HeaderStyle>
    <Columns>
        <asp:TemplateField HeaderText="">
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="25" />
            <ItemTemplate>
                <asp:LinkButton ID="lnkFirst" CommandName="FirstCommand" CommandArgument='<%# Eval("MyID") %>' runat="server" Text='First'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="data1" HeaderText="Data1"><ItemStyle Width="70px"></ItemStyle></asp:BoundField>
        <asp:BoundField DataField="data2" HeaderText="Data2"></asp:BoundField>
        <asp:TemplateField HeaderText="">
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="25" />
            <ItemTemplate>
                <asp:LinkButton ID="lnkSecond" CommandName="SecondCommand" CommandArgument='<%# Eval("MyID") %>' runat="server" Text='Second'></asp:LinkButton>
            </ItemTemplate>
         </asp:TemplateField>
    </Columns>
</asp:GridView>

在我后面的代码中:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        If Session("SpecialRole") <> "First" Then
            gvMyGrid.Columns.RemoveAt(0) 'Row Command fires if I comment this out
        End If

        BindMyGrid()
    End If
End Sub

0 个答案:

没有答案
相关问题