How we can easily get value from viewstate at client side

Viewstate might be very useful during Asp.net application, it can hold the value until you are on the page means when you redirected to another page all saved value will be removed.

Its very easy to use viewstate value on code behind, but if you will try to get on client side it might not be simple using jquery, so today I will show you How to use viewstate value on client side in asp.net

How we can easily get value from viewstate at client side

The way I am using is very simple, I am not going to get value using hidden field, you might know, each viewstate key will be created as new hidden field starting with underscore ( _ ). You don’t need to bother about it.

I used server side code on client side in order to get value from Viewstate, its very simple to use.
Example are as follows.

<% ViewState["Total"] = "10"; %>

$(document).ready(function () {
   alert('5 + 5 = '+ '<%= ViewState["Total"].ToString() %>')
});

You can see, I first initialize Total key with value 10 in Veiwstate. After that I am alerting on client side. You can see how easy it is.

If you have any question feel free to ask.

Leave a Reply