How to access viewstate using javascript?

You can simply access the hidden form element that holds the viewstate.

The name of the control is __viewstate.

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="..." />

var vCode = documents.forms[0]['__VIEWSTATE'].Value;
alert(dateView);

Of course, this will give you the encrypted/encoded/compressed viewstate.

If you want specific values from it, you may find it better to record them in hidden fields and access those.


I would suggests to use RegisterHiddenField than mixing server/js codes:

You may try this sample:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ViewState("code") = "EE"
    Page.ClientScript.RegisterHiddenField("vCode", ViewState("code"))
End Sub

On your javascript:

var vCode = document.getElementById("vCode");
alert(vCode);