locked
webcontrol loosing viewstate information during postbacks RRS feed

  • Question

  • User2046362424 posted

    Hi,

    I am having tough time finding out this reason. I have a web control where i add some variable in viewstate. like viewstate["test"] = "1".

    when i post back this viewstate no more exists in collection. any idea? here is my code

    public class mycontrol:WebControl { public mycontrol() { // // TODO: Add constructor logic here // } protected override void OnInit(EventArgs e) { Page.RegisterViewStateHandler(); //this.TrackViewState(); //Page.RegisterRequiresControlState(this); //ViewState["initstate"] = "1"; //ViewState.Add("tedt", "tt"); //this.EnableViewState = true; base.OnInit(e); } protected override void OnPreRender(EventArgs e) { ViewState["renderstate"] = "1"; //SaveViewState(); base.OnPreRender(e); } protected override object SaveControlState() { //return base.SaveControlState(); } }


    Saturday, February 5, 2011 11:15 AM

Answers

  • User-158764254 posted

    if you override a method but dont call the base method, you can break functionality.  check you override of Save ControlState for example.

    But there should be no need to add all the overrides you've added, you can read/write to viewstate directly

    I often like to wrap up the viewstate access in a property.

    public int SomeInteger {

        get {

            object o = ViewState["SomeInteger"];

            if (o != null) return (int)o;

            return 0;

            //a default

        }

        set { ViewState["SomeInteger"] = value; }

    }

     

    http://wiki.asp.net/page.aspx/58/viewstate/ 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 5, 2011 12:17 PM