Javascript 如何使用javascript访问视图状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6980714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:06:26  来源:igfitidea点击:

How to access viewstate using javascript?

javascriptasp.netviewstate

提问by Sukhjeevan

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?

有人建议我怎么做吗?

回答by jerjer

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

我建议使用 RegisterHiddenField 而不是混合服务器/js 代码:

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:

在您的 javascript 上:

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

回答by Oded

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

您可以简单地访问保存视图状态的隐藏表单元素。

The name of the control is __viewstate.

控件的名称是__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.

如果您想从中获取特定值,您可能会发现将它们记录在隐藏字段中并访问它们会更好。

回答by Niloofar

The Page.ClientScript.RegisterHiddenField did not work for me and returned null. You can do like this:

Page.ClientScript.RegisterHiddenField 对我不起作用并返回 null。你可以这样做:

1-First solution:define a hidden field and make sure you set runat=server

1-第一个解决方案:定义一个隐藏字段并确保设置 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:

在您的 javascript 中像这样访问它:

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

2-Second solution

2秒解决方案

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

然后像往常一样在javascript中访问它

   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

注意:如果您使用更新面板,请将隐藏字段放在更新面板的 contenttemplate 标签内