GridView RowCommand Not Firing After Deleting Column

100 Views Asked by At

I have a GridView with some data and a couple LinkButton columns. If the user doesn't have a specific role, I remove one of the LinkButton columns from the GridView.

My problem is that when I remove the column, the RowCommand event won't fire anymore and all the LinkButtons disappear when I click them.

Note: I'm not re-binding the grid on PostBack, I'm not disabling ViewState, and I tried setting CausesValidation="false" with no change. Everyone I've found online with this problem has had one of those issues.

If I comment out the code that removes the column, everything works without issue.

EDIT: If I hide the column instead of removing it with gvMyGrid.Columns(0).Visible = False, everything works.

Why would removing the column prevent that event from firing?

Here's my 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>

In my code behind:

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

There are 0 best solutions below