0

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.

8
  • 1
    I'm not totally sure, but it looks like you have a lot of unnecessary code, such as declaring cntrl and assigning it a reference to an existing control. You declare container but never use it. I would extract all the code to change visibility of controls to a separate Sub, with a parameter isVisible.
    – HardCode
    Commented May 8 at 15:40
  • I added that code to try another way to set Control Visible to True but no matter what the control in code debugger is false and the controls don't display. Any idea how to fix this?
    – dibello.4
    Commented May 9 at 12:58
  • What is ToggleMenuButtonSelection() doing? Also, revise your question to clearly state the desired behavior. As of now, it just says what is happening and not what you want it to do.
    – HardCode
    Commented May 9 at 15:18
  • I want all the controls I am setting to True to be true. Currently these Controls remain False after setting them to True in code behind.
    – dibello.4
    Commented May 10 at 12:32
  • Public Shared Sub ToggleMenuButtonSelection(ByVal selectedButton As Button, ByVal cssClassBaseName As String) For Each ctrl As Control In selectedButton.Parent.Controls If TypeOf ctrl Is Button Then If ctrl.ID = selectedButton.ID Then CType(ctrl, Button).CssClass = cssClassBaseName + " " + cssClassBaseName + "_selected" Else CType(ctrl, Button).CssClass = cssClassBaseName End If End If Next End Sub
    – dibello.4
    Commented May 10 at 12:41

2 Answers 2

0

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.

0

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?

Not the answer you're looking for? Browse other questions tagged or ask your own question.