viewstate vs hiddenfield in

Posted by Krishnamanohar under ASP.NET on 8/1/2014 | Points: 10 | Views : 17625 | Status : [Member] | Replies : 2
what is the difference between viewstate and hiddenfield with suitable examples in
Thanks in Advance


Posted by: kgovindarao523-21772 on: 8/1/2014 [Member] [MVP] Bronze | Points: 25


The View State is client state mechanism in the ASP.Net statement management.

The view state is control it will be used to maintain the state of the control across the posted back to the server. The value will be stored in the hidden control. Every time it needs to encryption and decryption (serialize and deserialize across the post backs).

The view state can be enabled by setting the property called EnableViewState="true/false". It can be set in the machine config/ web config/ page directives/ control level.

By default it is false. You can enable it by setting boolean flag.

This can be used with in the page and not across the page.

Viewstate["Version"] = txtVersionName.Text.ToString();

The view state also can store the data using the key and value combination. The data will be serialized and stored in the hidden control You can view the source code page and find the hidden control with _viewstate.

- Increase the page payload (when you have grid with many rows then every thing has to be loaded and retain again with serialize and deserilize)
- Additional overhead while serialize and deserialize.
- Increase the memory allocation on the server.

You can see the assigning and retrieval of view state in

protected void Button1_Click(object sender, System.EventArgs e) 
ViewState["FavoriteColor"] = TextBox1.Text.ToString();
ViewState["City"] = TextBox2.Text.ToString();
Label1.Text = "Your data saved in ViewState.";
protected void Button2_Click(object sender, System.EventArgs e)
string color = (string)ViewState["FavoriteColor"];
string city = (string)ViewState["City"];

Label1.Text = "Hi your favorite color is: " + color +
and you came from: " + city;

The HiddenField control provides you with a way to store information in the page without displaying it. For example, you might store a user-preference setting in a HiddenField control. To put information into a HiddenField control, you set itsValue property to the value you want to store between postbacks.

As with any other Web server control, the information in a HiddenField control is available during postback. The information is not persisted outside the page.

The ASP.Net Hiddenfield will be created like this.

Tht HTML hidden field will be defined like this.

If you want to assign the value from the server side then

hidName.Value = "Testing";

Thank you,

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Manimaddu on: 8/12/2014 [Member] Starter | Points: 25

In hidden field and Viewstate both manage memory management.
we can store value in hidden field and viewstate also.
now lets check the difference between both

1->Hidden Field is server control and Viewstate is variable .
2->Hidden field won't visible in browser same for Viewstate but if we will use hidden field ,
we will get more extra encrypted code in view source whereas if we will use Viewstate we will not get that much of extra code.
3-><input type="hidden" id="hdn" runat="server">
To assign value to hidden field .. use value property of hidden field
hdn.Value="sometext" // assingning value to hidden field
lablet.text=hdn.value // storing value of hidden field into variable

For viewstate
Viewstate("storeval")="sometext" // assigning value into viewstate
string str=Viewstate("storeval") //storing value of viewstate into variable
in c# syntax is different.

so in these way we can use hidden field and view state.
hidden field go to the server but Viewstate maintain after postback whithout touching server.

Thanks & Regards,
Mani Kumar

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response