如何在客户端Javascript或JQuery中访问ViewState变量

时间:2022-02-15 16:18:57

Is this possible to access ViewState variable at Client Side javascript or jquery functions in asp.net web application? If yes then how?

这是否可以在asp.net Web应用程序中访问Client Side javascript或jquery函数中的ViewState变量?如果是,那怎么样?

1 个解决方案

#1


8  

First Solution:

You can pass any variable from codebehind to client-side using properties. Define a Public propery in codebehind:

您可以使用属性将任何变量从代码隐藏传递到客户端。在代码隐藏中定义公共属性:

C#:

public int prtPropertyName {
    get { return ViewState("PropertyName"); }
    set { ViewState("PropertyName") = value; }
}


VB:

Public Property prtPropertyName As Integer
    Get
        Return ViewState("PropertyName")
    End Get
    Set(value As Integer)
        ViewState("PropertyName") = value
    End Set
End Property

assign a value to the property and then get the value in javascript using this:

为属性赋值,然后使用以下方法在javascript中获取值:

<% = prtPropertyName  %>


Second Solution:

Put the ViewState's value in a hidden field and read the hidden field value in client-side:

将ViewState的值放在隐藏字段中,并在客户端读取隐藏字段值:

ViewState("viewStateName") = "This is ViewState value"
Page.ClientScript.RegisterHiddenField("hfHiddenFieldID", ViewState("viewStateName"))

Javascript:

var strValue = document.getElementById("hfHiddenFieldID");


Third Solution:

This one is not so clear but all ViewStates is saved in a hidden field that created by ASP.NET automatically, you can find the field and read data. You can find this fields in source code of page with this name and id: name="__VIEWSTATE" id="__VIEWSTATE".

这个不太清楚,但所有ViewStates都保存在ASP.NET自动创建的隐藏字段中,您可以找到该字段并读取数据。您可以在页面的源代码中找到具有此名称和id的字段:name =“__ VIEWSTATE”id =“__ VIEWSTATE”。

#1


8  

First Solution:

You can pass any variable from codebehind to client-side using properties. Define a Public propery in codebehind:

您可以使用属性将任何变量从代码隐藏传递到客户端。在代码隐藏中定义公共属性:

C#:

public int prtPropertyName {
    get { return ViewState("PropertyName"); }
    set { ViewState("PropertyName") = value; }
}


VB:

Public Property prtPropertyName As Integer
    Get
        Return ViewState("PropertyName")
    End Get
    Set(value As Integer)
        ViewState("PropertyName") = value
    End Set
End Property

assign a value to the property and then get the value in javascript using this:

为属性赋值,然后使用以下方法在javascript中获取值:

<% = prtPropertyName  %>


Second Solution:

Put the ViewState's value in a hidden field and read the hidden field value in client-side:

将ViewState的值放在隐藏字段中,并在客户端读取隐藏字段值:

ViewState("viewStateName") = "This is ViewState value"
Page.ClientScript.RegisterHiddenField("hfHiddenFieldID", ViewState("viewStateName"))

Javascript:

var strValue = document.getElementById("hfHiddenFieldID");


Third Solution:

This one is not so clear but all ViewStates is saved in a hidden field that created by ASP.NET automatically, you can find the field and read data. You can find this fields in source code of page with this name and id: name="__VIEWSTATE" id="__VIEWSTATE".

这个不太清楚,但所有ViewStates都保存在ASP.NET自动创建的隐藏字段中,您可以找到该字段并读取数据。您可以在页面的源代码中找到具有此名称和id的字段:name =“__ VIEWSTATE”id =“__ VIEWSTATE”。