I am trying to access view-state in client side but following exception coming :
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?
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'].
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With