I made a UserControl that implements the “Like” behavior, facebook style.
When hitting “Like” (or “Unlike”), the control sends a request to an Ajax enabled WCF service, which in turn manually renders the usercontrol and sends the result back to the client.
It all works well, however it causes other controls in the same page to generate the following JavaScript error:
Message: Sys.WebForms.PageRequestManagerServerErrorException: The state information is invalid for this page and might be corrupted.
URI: http://somesite/ScriptResource.axd?d=GF84PA...
The error occurs only if I press the like/unlike buttons, and then tries something on the page that requires a callback, such as paging in a ListView or submitting a comment.
I’m using C#, ASP.NET, .NET 3.5.
Edit:
My code of rendering the UserControl in WCF looks like this:
public string RenderHtml()
{
var pageHolder = new Page();
var formControl = new HtmlForm();
var userControl = (TControl)pageHolder.LoadControl(userControlPath);
if (setControlPropertiesDelegate != null)
setControlPropertiesDelegate(userControl);
formControl.Controls.Add(userControl);
pageHolder.Controls.Add(formControl);
var output = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, output, false);
var outputString = output.ToString();
return outputString;
}
I found out that since I’m rendering the control inside a page and a form, a ViewState hidden field is created (and thus sent to the client through WCF).
I added a method just before the return statement, that removes the form tag and the ViewState, so only the rendered control string is being sent. Now it works just fine.
All controls use ViewState to store properties and settings. Even when you turn it off manually, there’s still a small amount of ViewState that will be generated internally by the control. If you are returning the rendered control’s HTML from a WCF service, then post the page back to the server, the error will be thrown because the Page knows nothing about the control you loaded dynamically when it decrypts the ViewState to rebuild the page.
As you mention in the comment to your question, manually stripping out the ViewState before sending the HTML down to the client will fix your problem.
I highly recommend reading this article on ViewState by Dave Reed. It is by far the best explanation of exactly how ViewState works (and what misconceptions you may have about it).