从 Javascript 设置 Viewstate 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24215993/
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
Set value of Viewstate from Javascript
提问by user3739515
I am working on asp.net application. Where for a page I have to clear the value of the controls(like textbox and dropdownlist,etc), also i have to remove the value of a ViewState. Initailly i was doing it from codebehind, so o problem was there. But now the problem arise when i tried reset the value of controls using client side , say
我正在研究 asp.net 应用程序。对于页面,我必须清除控件的值(如文本框和下拉列表等),还必须删除 ViewState 的值。最初我是从代码隐藏中做的,所以问题出在那里。但是现在当我尝试使用客户端重置控件的值时出现了问题,比如说
document.getElementById('<%= txtTextBox.ClientID %>').value ='';
but the problem i am facing is that i cannot set the viewstate value from clientside. my i have to clear to viewstate one is a simple variable say ViewState["NameOfUser"],and another one is converting a datatable into viewstate say,
但我面临的问题是我无法从客户端设置视图状态值。我必须清除视图状态,一个是简单的变量,比如 ViewState["NameOfUser"],另一个是将数据表转换为视图状态,比如,
ViewState["dataTable"] = dt;
Thanks and regards
谢谢并恭祝安康
采纳答案by DOTNET Team
You simply can not assign value in client side.
您根本无法在客户端分配值。
- Either you have to pass it through hidden field while submitting form or
- Call ajax from javascript and access the ViewState server side.
- 您必须在提交表单时通过隐藏字段或
- 从javascript调用ajax,访问ViewState服务器端。
回答by José Samuel Coelho
I found this way via ajax request:
我通过ajax请求找到了这种方式:
Credits: https://forums.asp.net/t/1991868.aspx?Set+value+of+Viewstate+from+Javascript+or+Jquery
学分:https: //forums.asp.net/t/1991868.aspx?Set+value+of+Viewstate+from+Javascript+or+ Jquery
ViewState["dataTable"] = dt;
Javascript:
Javascript:
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(function () {
$("#Button1").click(function () {
//clear datatable
$.ajax({
type: "POST",
url: "WebForm1.aspx/ClearDataTable",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert('cleared');
}
});
//clear other values.....
//............
});
})
</script>
On codebehind:
在代码隐藏上:
[WebMethod]
public static void ClearDataTable()
{
HttpContext.Current.Session["datatable"] = null;
}
Hope that it helps!
希望它有帮助!