Search code examples
asp.netpostbackpanelviewstatevisible

How to flip a panel's visiblity on postback?


I have an asp.net panel control which I want to appear on every other page load for a user, so I thought I could just change the Visible property in the page_load like so:

panel.Visible = !panel.Visible.

However every time this runs the Visible has changed back to true. Is the state of the panel not maintained with viewstate?


Solution

  • In the end I used session state to get around this.

    I stored a bool in session to track whether the panel was visible or not, then in the page load method set the Visible property of the panel to the opposite of what was stored in session.