Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access viewstate using javascript?

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?

like image 373
Sukhjeevan Avatar asked Aug 08 '11 10:08

Sukhjeevan


People also ask

How do you get ViewState?

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'].

How do you use client side Viewtate?

So using the View State we store the page value during the page post-back. View State is a 64 bit serialized string code in hidden format on the page and when the user makes a request then this data moves from the server to the client and the client to the server depending on the user request.

Can we use ViewState in another page?

You can't access ViewState of one page from another page directly. If you want to access a particular ViewState value then you can pass the value in Context collection and then access the value in other page.

Where is the ViewState information stored?

By default, view state data is stored in the page in a hidden field and is encoded using base64 encoding. In addition, a hash of the view state data is created from the data by using a machine authentication code (MAC) key.


1 Answers

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);
like image 170
jerjer Avatar answered Oct 07 '22 09:10

jerjer