在 VB.NET 按钮单击事件中,我无法设置 Visible = true。在下面代码的所有示例中,Control 可见属性都设置为 False,尽管代码将其设置为 True?有谁知道如何解决这一问题。我尝试将所有父控件设置为可见。
Public Sub btnReports_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnReports.Click
If Not IsAllowed(_contextAppUser, btnReports) Then
Exit Sub
End If
ToggleMenuButtonSelection(btnReports, "idec_mainmenu_button")
RememberMenuSelection(Me, btnReports, (New StackFrame).GetMethod())
InitializeReportsView()
Dim Set1 As Boolean = True
If upnlMainMenu.Visible = False Then
upnlMainMenu.Visible = Set1
End If
Dim container As Control
Dim cntrl As Control = upnlMainMenu.FindControl("tvReportNavigation")
cntrl.Visible = True
If upnlMainMenu.Visible = False Then
upnlMainMenu.Visible = Set1
End If
If tblcellMainMenu.Visible = False Then
tblcellMainMenu.Visible = Set1
End If
If dgFileListing.Visible = False Then
dgFileListing.Visible = Set1
End If
If mvMainContent.Visible = False Then
mvMainContent.Visible = Set1
End If
If vwReports.Visible = False Then
vwReports.Visible = Set1
End If
If tvTrainerDocs.Visible = False Then
tvTrainerDocs.Visible = Set1
End If
If pnlReportNav.Visible = False Then
pnlReportNav.Visible = Set1
End If
If tvReportNavigation.Visible = False Then
tvReportNavigation.Visible = Set1
End If
If pnlFileListing.Visible = False Then
pnlFileListing.Visible = Set1
End If
If dgFileListing.Visible = False Then
dgFileListing.Visible = Set1
End If
Me.vwReports.Visible = True
Visible 应设置为 True。
如果目标是改为可见,我看不出测试可见有任何优势。
因此这应该足够了:
upnlMainMenu.Visible = Set1
tblcellMainMenu.Visible = Set1
dgFileListing.Visible = Set1
mvMainContent.Visible = Set1
vwReports.Visible = Set1
tvTrainerDocs.Visible = Set1
pnlReportNav.Visible = Set1
tvReportNavigation.Visible = Set1
pnlFileListing.Visible = Set1
dgFileListing.Visible = Set1
vwReports.Visible = True
但是,您可能需要提供一些标记。如果此类控件通过代码注入到页面中,那么在大多数情况下,控件的视图状态将不会在回发之间持续存在。
如果控件不是服务器端控件,则它们的视图状态(以及因此它们的可见设置将无法支持(保留)回发。
但是,您可以使用 runat = "server" 标记 html 元素,因此此类控制不仅可以被后面的代码看到,而且还会自动为您分配视图状态。
因此,这个标记:
<asp:Button ID="Button1" runat="server" Text="toggle visible"
OnClick="Button1_Click" CssClass="btn"
/>
<asp:Button ID="Button2" runat="server" Text="Turn all visible"
OnClick="Button2_Click" CssClass="btn"
style="margin-left:35px"
/>
<asp:Button ID="Button3" runat="server" Text="Hide all"
OnClick="Button3_Click" CssClass="btn"
style="margin-left:35px"
/>
<br />
<div id="div1" runat="server">
<h3>This is div 1</h3>
</div>
<div id="div2" runat="server">
<h3>This is div 2</h3>
</div>
<div id="div3" runat="server">
<h3>This is div 3</h3>
</div>
背后的代码:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs)
div1.Visible = Not (div1.Visible)
div2.Visible = Not (div2.Visible)
div3.Visible = Not (div3.Visible)
End Sub
Protected Sub Button2_Click(sender As Object, e As EventArgs)
div1.Visible = True
div2.Visible = True
div3.Visible = True
End Sub
Protected Sub Button3_Click(sender As Object, e As EventArgs)
div1.Visible = False
div2.Visible = False
div3.Visible = False
End Sub
现在的结果是这样的: