Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can i set ViewState["Drivers"] to null using jQuery in ASP.NET ?

C#
ViewState["Drivers"]= null;
Posted

1 solution

You should know that ViewState (and other MVC's objects) are server-side and are manipulated on the run-time, after the content is sent (in HTML form) to the client, there is no connection to those objects anymore. To manipulate the DOM then, you have to use JavaScript (or jQuery specifically) and you alter the state of HTML content.

To change the value, you make another request to the server (generally with a QueryString to tell the server that you want to change the value or you want to do something instead of just getting the values or static HTML page). jQuery allows you to create an ajax request. You can send this request, to the server, and handle the request (inside the controller) to change the ViewState.

JavaScript
$(document).ready(function () {
   $.ajax({
      url: 'your_controller/update_viewstate',
      success: function (data) {
         // value altered..
      }
   });
});


Now, on the server-side inside the update_viewstate action, you can change the ViewState as you want it (inside your post). Then you will have to change the View too, because View's dependencies have been altered and so on. So I would say, this is a lame method to alter the View's properties. You can simply change the HTML content if you have to.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900