Viewstate(2)

"After postback, the values will be there still, unless ViewState is explicitly disabled by the user. This is the way all the .NET WebControls work (try setting the text of a control in onload when !IsPostBack and notice how it will retain its value for the lifetime of the page, even though you never explicitly set it again).

You can read the value of viewstate from debug mode probably because it is using Reflection to examine the class. Reflection allows you to read both protected and private class members when the appropriate BindingFlags are set.

I don't know of any article that explains using viewstate between controls other than what I have already said about using a property that gets and sets a viewstate member. That is how you should always do it. You should never access the viewstate directly."

[Jesse]


Jesse, some misunderstanding there , because I was thinking that you were talking about another method than Viewstate.
However, I still not understand how you find a way to write the Page viewstate from the control (I know I can use Reflection).
Another way I found is also to use Context.http to pass some values.
What do you think about that ?


1 Comment

  • From within the control, you can use properties to write to viewstate:





    public class MyControl : WebControl


    {


    public string MyValue


    {


    get


    {


    return (string)ViewState["MyValue"];


    }


    set


    {


    ViewState["MyValue"] = value;


    }


    }





    public void SomeMethod()


    {


    MyValue = "Something"; // set persistent property value


    }


    }





    from outside the class, you use the properties as well:





    public class OtherClass


    {


    public void SomeMethod()


    {


    MyControl c = new MyControl();


    c.MyValue = "something"; // Set persistent property value


    }


    }

Comments have been disabled for this content.