【问题标题】:Why can't I change the display of Edit and Delete buttons in GridView?为什么无法更改 GridView 中编辑和删除按钮的显示?
【发布时间】:2019-09-06 19:29:59
【问题描述】:

在 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>

我希望每一行都开始启用编辑和删除按钮。单击一行的编辑按钮后,我希望每隔一行都禁用或隐藏编辑和删除按钮。更新行后,我希望所有行都启用了编辑和删除按钮。

【问题讨论】:

    标签: vb.net gridview findcontrol


    【解决方案1】:

    【讨论】:

      猜你喜欢
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多