0

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
TheIronCheek
  • 927
  • 1
  • 13
  • 41
  • 1
    Instead of using the RemoveAt function, try hiding it to see if you have the same result. You can use `gvMyGrid.Columns[0].Visible = False (or similar for VB.NET). If you do experience the isue, you know it may be something else causing the issue and not the RemoveAt function. – Jack Oct 18 '18 at 20:21
  • @Jack - Switching to `gvMyGrid.Columns(0).Visible = False` fixed the issue. I'll rephrase the question to ask *why* removing the column causes the event not to fire. – TheIronCheek Oct 18 '18 at 20:33
  • You can also try Gridview.Columns(0).Remove instead. It is supposed to do the same thing as RemoveAt, but it might be worth a shot. – Jack Oct 18 '18 at 20:59

0 Answers0