admin管理员组文章数量:1416631
I am working on asp 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
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["dataTable"] = dt;
Thanks and regards
I am working on asp 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
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["dataTable"] = dt;
Thanks and regards
Share Improve this question asked Jun 14, 2014 at 2:08 user3739515user3739515 211 gold badge1 silver badge3 bronze badges 4- So what's the question here? – Rahul Commented Jun 14, 2014 at 2:41
- If you change the element the javacode included by microsoft will change the viewstate on submit. – Hogan Commented Jun 14, 2014 at 2:54
- @Rahul is it not clear from the title ?? – user3739515 Commented Jun 14, 2014 at 4:59
- You shouldn't modify viewstate, as that will give you an error when posting back... – cracker Commented Jun 14, 2014 at 11:32
2 Answers
Reset to default 1You 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.
I found this way via ajax request:
Credits: https://forums.asp/t/1991868.aspx?Set+value+of+Viewstate+from+Javascript+or+Jquery
ViewState["dataTable"] = dt;
Javascript:
<script src="//code.jquery./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!
本文标签: cSet value of Viewstate from JavascriptStack Overflow
版权声明:本文标题:c# - Set value of Viewstate from Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745252439a2649903.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论