skip to Main Content

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


  1. Chosen as BEST ANSWER

    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?


  2. I fail to see any advantage to testing visible if the goal is to change to visible anyway.

    Hence this should suffice:

        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
    

    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:

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

    And code behind:

    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
    

    And the result is now this:

    enter image description here

    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:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        If Not IsPostBack Then
    
            div1.Visible = False
            div2.Visible = True ' ONLY the middle div visible
            div3.Visible = False
    
        End If
    
    End Sub
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search