How to access the view-state values in JavaScript? [Resolved]

Posted by Programmer123 under JavaScript on 9/26/2013 | Points: 10 | Views : 10228 | Status : [Member] | Replies : 4
How to access the view-state values in JavaScript?




Responses

Posted by: Bandi on: 9/27/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Use RegisterHiddenField than mixing server/js codes
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);


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/27/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
The answer is NO, You can access the Hidden field "__ViewState" through JavaScript but it is all encrypted.

If you want to get the encrypted value using JavaScript you can do the below code:-

function GetData()
{
var data=document.getElementById('__VIEWSTATE').value;
alert(data);
return false;
}


Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER





Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/27/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
There is an alternative way to access the View-State Values form different method:-

Define a hidden field and make sure you set runat=server

<input type="hidden"  id="myhiddenField" runat="server" value="" />


then in your code behind assign any value you want to it

myhiddenField.Value= ViewState["name"].ToString();// or assign any value you want


In your javascript access it like this:

<script type="text/javascript">  
function test()
{
var name = document.getElementById('myhiddenField').value;
alert(name)
}
</script>


Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/27/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Another way:-

In case for some reasons you don't want to have a server input control you can put the hidden field in a literal tag

<asp:literal id="literal1" runat="server"><input type="hidden" id="myhiddenField" value="{0}"/></asp:literal>


and then assign a value to the literal in codebehind like this

literal1.Text = string.Format(literal1.Text, "somevalue");  // somevlue can be your ViewState value



then access it in javascript as usual

var name = document.getElementById('myhiddenField').value;
alert(name)


Note: if you are using update panels put the hiddenfields inside the contenttemplate tag of the updatepanel

Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response