How to access viewstate using javascript?

Sukhjeevan picture Sukhjeevan · Aug 8, 2011 · Viewed 82.4k times · Source

I am trying to access view-state in client side but following exception coming : enter image description here

JAVASCRIPT:

<script language="javascript" type="text/javascript">
    var vCode = '<%=ViewState("code")%>';
    alert(dateView);
</script>

CODE BEHIND:

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

Anybody suggest me how to do it?

Answer

jerjer picture jerjer · Aug 8, 2011

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);