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
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!

本文标签: cSet value of Viewstate from JavascriptStack Overflow