vb.netgridviewfindcontrol

Why can't I change the display of Edit and Delete buttons in GridView?


In VB.Net I am using a gridview to display some information. For this gridview, I am using the ItemTemplate and the EditItemTemplate.

What I want to do is stop the user from being able to click the Edit or Delete buttons of another row while the are in edit mode of a row. Disabling or hiding will work for what I want to do. After the user clicks cancel or after the data for the row has been updated, I would like to re-enable the buttons.

I have tried hiding the column using MyGridView.columns(6).Visible = False I have also tried to access the buttons by command name using

For i As Integer = 0 To gvUserDetails.Rows.Count - 1
        If i <> gvr.RowIndex Then
            gridRow = gvUserDetails.Rows(i)
            For Each cell As Control In gridRow.Cells
                For Each ctl As Control In cell.Controls
                    If TypeOf ctl Is Button Then
                        Dim commandButton As Button = CType(ctl, Button)
                        If commandButton.CommandName = "Edit" Then
                            editButton = commandButton
                            'editButton.Enabled = False
                            editButton.Visible = False
                        ElseIf commandButton.CommandName = "Delete" Then
                            deleteButton = commandButton
                            'deleteButton.Enabled = False
                            deleteButton.Visible = False
                        End If
                    End If
                Next
            Next
        End If

I have tried to hide and disable the buttons but I'm not sure where I am going wrong. I have also stepped through using the Visual Studio debugger. I can see that my code is finding the buttons. After a change I look at the property window to confirm. But when the code is complete and the page renders, I don't see that anything has been hidden or disabled.

 Protected Sub btnEditNotification_Click(sender As Object, e As EventArgs)
        btnCreateUser.Enabled = False
        btnAddNotification.Enabled = False
        gvCurrentUsers.Columns(4).Visible = False
        gvCurrentUsers.Columns(0).Visible = False

        'disable edit and delete commands for other rows
        Dim Btn As Button = CType(sender, Button)
        Dim gvr As GridViewRow = Btn.NamingContainer
        Dim editButton As Button = Nothing
        Dim deleteButton As Button = Nothing
        Dim gridRow As GridViewRow

        For i As Integer = 0 To gvUserDetails.Rows.Count - 1
            If i <> gvr.RowIndex Then
                gridRow = gvUserDetails.Rows(i)
                For Each cell As Control In gridRow.Cells
                    For Each ctl As Control In cell.Controls
                        If TypeOf ctl Is Button Then
                            Dim commandButton As Button = CType(ctl, Button)
                            If commandButton.CommandName = "Edit" Then
                                editButton = commandButton
                                'editButton.Enabled = False
                                editButton.Visible = False
                            ElseIf commandButton.CommandName = "Delete" Then
                                deleteButton = commandButton
                                'deleteButton.Enabled = False
                                deleteButton.Visible = False
                            End If
                        End If
                    Next
                Next
            End If
        Next

    End Sub 
  <EditItemTemplate>

                        <asp:Button ID="ButtonUpdate" runat="server" CommandName="Update" Text="Update" CausesValidation="true"/>&nbsp;
                        <asp:Button ID="ButtonCancel" runat="server" CommandName="Cancel" Text="Cancel" OnClick="ButtonCancel_Click" />&nbsp;
                        <asp:Button ID="ButtonClearEndDate" runat="server" Text="Clear End Date" OnClick="ButtonClearEndDate_Click" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Button ID="btnEditNotification" runat="server" Text="Edit" CommandName="Edit" CausesValidation="False" OnClick="btnEditNotification_Click"/>
                        &nbsp;<asp:Button ID="btnDeleteNotification" runat="server" Text="Delete" CommandName="Delete" CausesValidation="False"  OnClientClick = " return confirm('Are you sure you want to delete this notification?');"/>
                    </ItemTemplate>

I expect every row to start out having enabled Edit and Delete buttons. After an Edit button for a row has been clicked, I expect every other row to have either disabled or hidden Edit and Delete buttons. After the row has been updated, I expect all rows to have enabled Edit and Delete buttons.


Solution

  • Read following MSDN Article, this should help. https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2008/ms171619(v=vs.90)