ViewState in user controls

Last post 03-26-2008 9:45 AM by whatispunk. 3 replies.

Sort Posts:

  • ViewState in user controls

    03-25-2008, 4:02 PM
    • Member
      307 point Member
    • Dovdimus Prime
    • Member since 03-25-2008, 7:59 PM
    • Bristol, UK
    • Posts 242

    Hi there

    I have a usercontrol with a number of properties. These properties are not getting 'remembered' between postbacks. I have tried setting the page's and the control's EnableViewState properties to true, but this doesn't make any difference.

     
    Two questions:

    1) How do I specify that the user control remembers its properties between postbacks?

    2) What is the control's EnableViewState property for?

     

    Thanks

     

    David 

  • Re: ViewState in user controls

    03-25-2008, 5:25 PM
    Answer
    • Contributor
      4,038 point Contributor
    • whatispunk
    • Member since 03-06-2007, 5:21 PM
    • Winnipeg
    • Posts 856

    You need to override SaveControlState() and LoadControlState().

    Here's an example:

     

     

    	private string _HeaderText;
    	public string HeaderText
    	{
    		get { return _HeaderText; }
    		set
    		{
    			_HeaderText = value;
    			lblHeaderText.Text = _HeaderText;
    		}
    	}
     
    	protected override object SaveControlState()
    	{
    		object[] controlState = new object[2];
    		controlState[0] = base.SaveControlState();
    		controlState[1] = _HeaderText;
    		return controlState;
    	}
    
    	protected override void LoadControlState(object savedState)
    	{
    		object[] controlState = (object[])savedState;
    		base.LoadControlState(controlState[0]);
    		_HeaderText = (string)controlState[1];
    	}
     
    Remember: mark posts that helped you as the answer to aid future readers

    Why UpdatePanels Are Dangerous
    Why You Should Not Place Your Whole Site In An UpdatePanel
  • Re: ViewState in user controls

    03-26-2008, 5:08 AM
    • Member
      307 point Member
    • Dovdimus Prime
    • Member since 03-25-2008, 7:59 PM
    • Bristol, UK
    • Posts 242

    Isn't it simpler to just use Properties to store the user control's members in ViewState?

    public string HeaderText

    {

    get

    {

    return (string)(ViewState["HeaderText"] ?? (object)"");

    }

    set

    {

    ViewState["HeaderText"] = value;

    }

    }

  • Re: ViewState in user controls

    03-26-2008, 9:45 AM
    Answer
    • Contributor
      4,038 point Contributor
    • whatispunk
    • Member since 03-06-2007, 5:21 PM
    • Winnipeg
    • Posts 856

    For control properties ControlState is a better location because it cannot be turned off by developers using your control. ViewState can be turned off however, so it should not be used for recording critical control information. So properties that are set in the controls markup, ControlState is a much better place to keep such information.

    http://msdn2.microsoft.com/en-us/library/1whwt1k7.aspx

    Remember: mark posts that helped you as the answer to aid future readers

    Why UpdatePanels Are Dangerous
    Why You Should Not Place Your Whole Site In An UpdatePanel
Page 1 of 1 (4 items)