MyDevBank

MyDevBank

summaries and tech views for new developers C#,asp.net for web\mobile development,databases,iis6 and more.
Creating this site was an idea that popped up after some programing years, when you have to get back to the same things you all ready forgot, while new stuff is arriving to town. In this site you can find summaries on tech stuff im into, so for a stable wide base knowledge pyramid ,enjoy!!

ViewState

HTTP POST is a way to submit data from the page to a different page on a server,
postback is an ASP.NET mechanisem to submit data to the same page.
ViewState is a collection of information generated by controls on an .aspx page that’s used by the controls to restore their state during a postback.
State in this context can include the values of control properties,
results of data binding,
or input from users.

on the server :
<form id="form1" runat="server">
</form>
generated HTML:
<form name="form1" method="post" action="viewstate1.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwULLTE2MTY2ODcyMjlkZExM0iM4ebB6mDGwogfzmhu/Ur0P" />
</div>
</form>

You can see the <input> tag containing the __VIEWSTATE field.
the value of the input field is a encoded to base64.
The browser sends the hidden field back to the server when the <form> is submitted, as it does with
all <input> fields.
When the runtime receives the field, it is decoded, deserialized, and used to restore the state of the controls.

ViewStateUserKey its a property which when used is helpfull for security,
by setting
protected void Page_Init(object sender, EventArgs e)
{
   this.ViewStateUserKey = Request.UserHostAddress;
}

The result is that the __VIEWSTATE hidden field will be different for each different IP address,
and users who submit the form from a different IP address will receive an error.
must be created in the Page_Init() event, which is before ViewState is restored in the page life cycle

saving a value to viewstate
void Page_PreRender(object sender, EventArgs e)
{
   ViewState.Add("MyVarName", MyVar);
}

when page.Ispostback = true
ViewState can be read and written only after the Page_Init() event and before Page_PreRender().

viewstate size think about a control that generates a big table with paging links ,
a control like that on the page can have a big viewstate connected to it resulting a long roundtrips.
in this case its beter to disable the viewstate EnableViewState="false" for the page or myControl.EnableViewState = false; for a control
and fatching the relevent data from the DB or
save the viewstate to the DB and provide a GUID to the page and viewstate.

Comment | Related Tags

HOME| IIS6 pipeline| ASP.NET pipeline| Http Modules| Http Handlers| Caching| Web Services| Page Life Cycle| Ajax| Jquery| Session State
ViewState| SEO| Mobile Basics| Mobile Handsets| C# Modifiers| C# Keywords| N-Tier| Design patterns| Stack & Heap| WCF
Databases| Relational Model| functionalities| Stored procedures| fishmarket
MyDevBank
© 2010 All Rights Reserved