In VB.NET Button click event I am unable to set Visible = true. In all examples in the code below the Control visible property is set to False although the code is setting it to True? Does anyone know how to fix this. I tried setting all parent controls to visible.
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 should be set to True.
2
Answers
I have tried in all events to set visibility to True but all the controls visibility remains false in the debugger and control is not displayed on the tree. How do you explain this?
I fail to see any advantage to testing visible if the goal is to change to visible anyway.
Hence this should suffice:
However, you probably need to provide some of the markup. If such controls are injected into the page with code, then in most cases, the view state of the control will not persist between post-backs.
If the controls are not server-side controls, then their view state (and thus their visible settings will not survive (persist) a post back.
However, you can tag html elements with runat = "server", and thus such controls not only are seen by code behind, but they are automatic assigned view state for you.
Hence, this markup:
And code behind:
And the result is now this:
Do keep in mind that for any post-back (button click etc.), then the page load event runs first, and runs each time, and then the code stub runs. This means that if you have setup code in the page load event, it should be setup to run only one time, and only on the first page load, not each time.
Hence, in near all cases, you need to enclose the page load code in a If Not IsPostBack stub.
Say, like this:
So, I setup the middle div to be visible, and top and bottom ones are hidden. However, note the all important If Not IsPostBack. If that first page load code is not enclosed in that IsPostBack test, then it will run each and every time, thus messing up any button code that runs after the page load event.
So, you are free to have setup code in the page load event, keeping in mind that the page load event fires each and every time, and such code runs each and every time before the button event code will run.
Hence, it becomes rather obvious that to build a working web page, then 99% if not near all of your web pages load event will require that PostBack test.