ViewState Vs Session … maintaining object through page lifecycle

If the search object isn’t huge in size, then go with using a ViewState. A ViewState is perfect if you only want the object to live for the current page’s lifecycle.

A session object is also fine to use, but obviously once the search object is in there, it will be around for longer the page’s lifecycle.

Also, one thing I do with ViewState/Session objects is wrap their access with a property:

public object GetObject
{
    get
    {
        return ViewState["MyObject"];
    }
    set
    {
        ViewState["MyObject"] = value;
    }
}

I tend to find it cleaner to do it this way. Just change the above code to fit your needs.

  • Source 1

  • Source 2

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)