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

问题描述 投票:0回答:1

在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个回答
© www.soinside.com 2019 - 2024. All rights reserved.