locked
Create a custom server control that have viewstate disabled by default RRS feed

  • Question

  • User2168155 posted

    I am trying to create server control that has its viewstate disabled by default. Will below code works for me?

    public class NoViewStateServercontrol: Control
    {
     public override bool EnableViewState
     {
      get
      {
       return false;
      }
      set
      {
       base.EnableViewState = value;
      }
     }
    }



    Tuesday, May 1, 2012 10:36 PM

Answers

  • User2168155 posted

    Firstly, thanks for the effort of chiragtoad. I have managed to discover this feature myself, here is how it is done:

    	public class NoViewStateLabel: Label
    	{
    		protected override void LoadViewState(object savedState)
    		{
    			if ( _viewStateEnabled)
    				base.LoadViewState(savedState);
    		}
    
    		private bool _viewStateEnabled = false;
    		public override bool EnableViewState
    		{
    			get
    			{
    				return base.EnableViewState;
    			}
    			set
    			{
    				_viewStateEnabled = value;
    				base.EnableViewState = value;
    			}
    		}
    	}

    Basicly, the System.Web.UI.Control class implement view state mechanism within itself by default, so we have to first make sure we are no longer relying on base class to track view state. So I adde a new variable "_viewStateEnabled" to track the view state separately. The allows us to only "LoadViewState" when the "EnableViewState" property of the control is explicitly turned on. Otherwise it will always be turned off (false) by default.

    Here is the test I used:

    	<custom:NoViewStateLabel ID="normal_label" runat="server" Text="Good" />
    	<custom:NoViewStateLabel ID="noviewstate_label" runat="server" Text="Good" />
    	<asp:Button ID="changelabelbackground" runat="server" 
    		Text="ChangeLabelBackground" OnClick="changelabelbackground_Click" />
    	<asp:Button ID="postback" runat="server" Text="PostBack"  />
    	protected void changelabelbackground_Click(object sender, EventArgs e)
    	{
    		normal_label.BackColor = System.Drawing.Color.Aquamarine;
    		noviewstate_label.BackColor = System.Drawing.Color.Aquamarine;
    	}

    The button "changelabelbackground" will change labels background color, after click this button both labels background color will be changed. However, click the "postback" button again, the "noviewstate_label" no longer preserves the background color change.




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 3, 2012 1:23 AM

All replies

  • User1304301175 posted

    check this link this may help you out

    http://msdn.microsoft.com/en-us/library/system.web.ui.control.enableviewstate.aspx

    http://stackoverflow.com/questions/186657/enable-viewstate-for-few-controls-and-disable-for-others-page

    Wednesday, May 2, 2012 1:29 AM
  • User2168155 posted

    Firstly, thanks for the effort of chiragtoad. I have managed to discover this feature myself, here is how it is done:

    	public class NoViewStateLabel: Label
    	{
    		protected override void LoadViewState(object savedState)
    		{
    			if ( _viewStateEnabled)
    				base.LoadViewState(savedState);
    		}
    
    		private bool _viewStateEnabled = false;
    		public override bool EnableViewState
    		{
    			get
    			{
    				return base.EnableViewState;
    			}
    			set
    			{
    				_viewStateEnabled = value;
    				base.EnableViewState = value;
    			}
    		}
    	}

    Basicly, the System.Web.UI.Control class implement view state mechanism within itself by default, so we have to first make sure we are no longer relying on base class to track view state. So I adde a new variable "_viewStateEnabled" to track the view state separately. The allows us to only "LoadViewState" when the "EnableViewState" property of the control is explicitly turned on. Otherwise it will always be turned off (false) by default.

    Here is the test I used:

    	<custom:NoViewStateLabel ID="normal_label" runat="server" Text="Good" />
    	<custom:NoViewStateLabel ID="noviewstate_label" runat="server" Text="Good" />
    	<asp:Button ID="changelabelbackground" runat="server" 
    		Text="ChangeLabelBackground" OnClick="changelabelbackground_Click" />
    	<asp:Button ID="postback" runat="server" Text="PostBack"  />
    	protected void changelabelbackground_Click(object sender, EventArgs e)
    	{
    		normal_label.BackColor = System.Drawing.Color.Aquamarine;
    		noviewstate_label.BackColor = System.Drawing.Color.Aquamarine;
    	}

    The button "changelabelbackground" will change labels background color, after click this button both labels background color will be changed. However, click the "postback" button again, the "noviewstate_label" no longer preserves the background color change.




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 3, 2012 1:23 AM
  • User2168155 posted

    And by the way if we set "EnableViewState=false" in control's "OnInit" method. This will disable the viewstate completely, even if we set "EnableViewState" to "true" in page markup.

    Thursday, May 3, 2012 2:16 AM