为什么我不能更改 GridView 中编辑和删除按钮的显示?
Why can't I change the display of Edit and Delete buttons in GridView?
在 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 调试器。我可以看到我的代码正在查找按钮。更改后我查看 属性 window 以确认。但是当代码完成并且页面呈现时,我没有看到任何东西被隐藏或禁用。
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"/>
<asp:Button ID="ButtonCancel" runat="server" CommandName="Cancel" Text="Cancel" OnClick="ButtonCancel_Click" />
<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"/>
<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 来显示一些信息。对于此 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 调试器。我可以看到我的代码正在查找按钮。更改后我查看 属性 window 以确认。但是当代码完成并且页面呈现时,我没有看到任何东西被隐藏或禁用。
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"/>
<asp:Button ID="ButtonCancel" runat="server" CommandName="Cancel" Text="Cancel" OnClick="ButtonCancel_Click" />
<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"/>
<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>
我希望每一行开始时都启用“编辑”和“删除”按钮。单击一行的编辑按钮后,我希望每隔一行都禁用或隐藏编辑和删除按钮。更新行后,我希望所有行都启用编辑和删除按钮。