Optimize Viewstate and Enum

In Asp.Net , When we use custom enumerations , and store them in the ViewState as Enum Text (Single or Married),
for eg.

 enum MyStatus {

Single = 0,

Married

}

The FullTypeName, i.e, name of the Type, the assembly Name, Strong name public key,

culture etc are stored in the viewstate. takeus up Almost 200 to 300 bytes

so it is always advised to cast it to integer before storing it in the viewstate

and parse it back to Enum while reading it.
eg.

 public MyStatus Status

{

get{ (MyStatus) Enum.Parse(typeof(MyStatus) , ViewState["MyStatus"]) ;

}

set

{

 ViewState["MyStatus"]  = (int) value;

}

}

Comments

  • Anonymous
    July 18, 2010
    Premature optimization is the root of all evil. IMO this is just like the ORM n+1 problem... overrated.

  • Anonymous
    July 19, 2010
    why do you think so, we have measured results to prove the same

  • Anonymous
    March 02, 2011
    it's ok.. you should not use the viewstate to store information like this go to lesson one!

  • Anonymous
    March 03, 2011
    Can you point to lesson one, link this is not a huge dataset, a simple enum

  • Anonymous
    July 07, 2011
    This doesn't work. Enum.Parse is expecting a string as the second parameter. ViewState[whatever] is an object.