Tuesday 13 March 2007

Updatepanel : Making other panels visble/invisible

Had a problem making multiple panels visible / invisible inside an UpdatePanel. The problem was occurring when I wanted to display different panels based on wether a user was logged in or not.
As an example I created the following user control with two panels which should alternatively be displayed.
When the Top button is clicked the code was storing in a session object that the user is logged in.When the Top button is clicked the code was storing in a session object that the user is logged out. During the Render method of the contorl if the seesion object was set to Logged In then the bottom panel was set visible=true and the top panel set to visible=false (and vice versa when logged out)
This worked, until another UpdatePanel on the page performed a partial postback, then both panels were being displayed.

Assumption of what was happening: The problem seems to be that the setting of the visible=true|false was being made in the wrong place. Setting the values in the Render method mean that they are not then stored on the control state; when a different updatepanel performs a partial postback it appears the control state is used to identify what elements of the page need updating. In the above case both panels has visible=true set in the control state.
Fix: To fix the problem the setting of the panel visibilty was moved to the PreRender method (after PreRender, SaveState is called, the Render).