Почему я не могу изменить отображение кнопок «Редактировать» и «Удалить» в GridView? - PullRequest
0 голосов
/ 16 апреля 2019

В VB.Net я использую gridview для отображения некоторой информации. Для этого вида сетки я использую ItemTemplate и EditItemTemplate.

Я хочу, чтобы пользователь не мог нажимать кнопки «Изменить» или «Удалить» другой строки, пока они находятся в режиме редактирования строки. Отключение или скрытие будет работать для того, что я хочу сделать. После того, как пользователь нажмет кнопку «Отмена» или после обновления данных для строки, я хочу снова включить кнопки.

Я пытался скрыть столбец, используя MyGridView.columns(6).Visible = False Я также пытался получить доступ к кнопкам по имени команды, используя

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

Я пытался спрятать и отключить кнопки, но я не уверен, в чем дело. Я также прошел через использование отладчика Visual Studio. Я вижу, что мой код находит кнопки. После изменения я смотрю на окно свойств, чтобы подтвердить. Но когда код завершен и страница отображается, я не вижу, чтобы что-то было скрыто или отключено.

 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>

Я ожидаю, что все строки начнутся с кнопок «Редактировать» и «Удалить». После нажатия кнопки «Изменить» для строки, я ожидаю, что в каждой строке будут либо отключены, либо скрыты кнопки «Изменить» и «Удалить». После обновления строки я ожидаю, что во всех строках будут включены кнопки «Редактировать» и «Удалить».

1 Ответ

0 голосов
/ 16 апреля 2019

Прочитайте следующую статью MSDN, это должно помочь.https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2008/ms171619(v=vs.90)

...