c# - Set value of Viewstate from Javascript - Stack Overflow

I am working on asp application. Where for a page I have to clear the value of the controls(like textbo

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
Add a ment  | 

2 Answers 2

Reset to default 1

You simply can not assign value in client side.

  1. Either you have to pass it through hidden field while submitting form or
  2. 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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745252440a4618758.html

相关推荐

  • c# - Set value of Viewstate from Javascript - Stack Overflow

    I am working on asp application. Where for a page I have to clear the value of the controls(like textbo

    2小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信