<p>I have a problem with maintaining state in an ASP.NET AJAX page. Short version: I need some way to update the page ViewState after an async callback has been made, to reflect any state changes the server made during the async call. </p>

<p>This seems to be a common problem, but I will describe my scenario to help explain:</p>

<p>I have a grid-like control which has some JavaScript enhancements - namely, the ability to drag and drop columns and rows. When a column or row is dropped into a new position, an AJAX method is invoked to notify the control server-side and fire a corresponding server-side event ("OnColumnMoved" or "OnRowMoved").</p>

<p>ASP.NET AJAX calls, by default, send the entire page as the request. That way the page goes through a complete lifecycle, viewstate is persisted and the state of the control is restored before the RaiseCallbackEvent method is invoked.</p>

<p>However, since the AJAX call does not update the page, the ViewState reflects the <em>original</em> state of the control, even after the column or row has been moved. So the second time a client-side action occurs, the AJAX request goes to the server and the page &amp; control are built back up again to reflect the <em>first</em> state of the control, not the state after the first column or row was moved.</p>

<p>This problem extends to many implications. For example if we have a client-side/AJAX action to add a new item to the grid, and then a row is dragged, the grid is built server-side with one less item than on the client-side.</p>

<p>And finally &amp; most seriously for my specific example, the actual data source object we are acting upon is stored in the page ViewState. That was a design decision to allow keeping a stateful copy of the manipulated data which can either be committed to DB after many manipulations or discarded if the user backs out. That is very difficult to change.</p>

<p>So, again, I need a way for the page ViewState to be updated on callback after the AJAX method is fired.</p>

[{'answer_id': 2379, 'author': 'Yaakov Ellis', 'author_id': 51, 'author_profile': 'https://Stackoverflow.com/users/51', 'pm_score': 1, 'selected': False, 'text': '<p>Check out this blog post: <a href="http://www.reimers.dk/blogs/jacob_reimers_weblog/archive/2007/03/23/tweaking-the-icallbackeventhandler-and-viewstate.aspx" rel="nofollow noreferrer">Tweaking the ICallbackEventHandler and Viewstate</a>. The author seems to be addressing the very situation that you are experiencing: </p>\n\n<blockquote>\n  <p>So when using ICallbackEventHandler you have two obstacles to overcome to have updated state management for callbacks. First is the problem of the read-only viewstate. The other is actually registering the changes the user has made to the page before triggering the callback.</p>\n</blockquote>\n\n<p>See the blog post for his suggestions on how to solve this. Also check out this <a href="http://forums.asp.net/t/1128218.aspx" rel="nofollow noreferrer">forum post</a> which discusses the same problem as well.</p>\n'}, {'answer_id': 2434, 'author': 'Rex M', 'author_id': 67, 'author_profile': 'https://Stackoverflow.com/users/67', 'pm_score': 0, 'selected': False, 'text': "<p>I actually found both of those links you provided, but as noted they are simply describing the problem, not solving it. The author of the blog post suggests a workaround by using a different ViewState provider, but unfortunately that isn't a possibility in this case...I really need to leave the particulars of the ViewState alone and just hook on to what is being done out-of-the-box.</p>\n"}, {'answer_id': 4910, 'author': 'Dave Ward', 'author_id': 60, 'author_profile': 'https://Stackoverflow.com/users/60', 'pm_score': 3, 'selected': True, 'text': "<p>If you're already shuffling the ViewState around anyway, you might as well use an UpdatePanel.  Its partial postbacks <strong>will</strong> update the page's ViewState automatically.</p>\n"}, {'answer_id': 5640, 'author': 'Rex M', 'author_id': 67, 'author_profile': 'https://Stackoverflow.com/users/67', 'pm_score': 0, 'selected': False, 'text': '<p>I found a fairly elegant solution with <a href="http://www.telerik.com/demos/aspnet/prometheus/Ajax/Examples/Common/ViewState/DefaultCS.aspx" rel="nofollow noreferrer">Telerik\'s RadAjaxManager</a>. It works quite nicely. Essentially you register each control which might invoke a postback, and then register each control which should be re-drawn after that postback is performed asynchronously. The <code>RadAjaxManager</code> will update the DOM after the async postback and rewrite the <code>ViewState</code> and all affected controls. After taking a peek in <code>Reflector</code>, it looks a little kludgy under the hood, but it suits my purposes.</p>\n'}, {'answer_id': 5684, 'author': 'Dave Ward', 'author_id': 60, 'author_profile': 'https://Stackoverflow.com/users/60', 'pm_score': 0, 'selected': False, 'text': "<p>I don't understand why you would use a custom control for that, when the built-in ASP.NET AJAX UpdatePanel does the same thing.</p>\n<p>It just adds more complexity, gives you less support, and makes it more difficult for others to work on your app.</p>\n"}]
['https://Stackoverflow.com/questions/2328', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/67/']