为什么不能在GridView中更改“编辑”和“删除”按钮的显示?

时间:2019-04-16 16:14:02

标签: vb.net gridview findcontrol

在VB.Net中,我正在使用gridview显示一些信息。对于此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 :(得分:0)