Saturday, February 2, 2008

Using Response.Redirect in ASP.NET Ajax Update Panel

Sys.WebForms.PageRequestManagerParserErrorException:
The message received from the server could not be parsed.
Common causes for this error are when the response is modified by calls to Response.Write(),
response filters, HttpModules, or server trace is enabled.
Details: Error parsing near'
<!DOCTYPE html PUB'.
 
Solution to the above exception is given below:
 
 Solution 1
private void RedirectTo(string url)
    {
      //url is in pattern "~myblog/mypage.aspx"
      string redirectURL = Page.ResolveClientUrl(url)
      string script = "window.location = '" + redirectURL + "';";
ScriptManager.RegisterStartupScript(this, typeof(Page), "RedirectTo", script, true);
    } 
 
 
 
Solution 2 (Recommended)
In web.config,  Add the key
 <httpModules>
     <add name="ScriptModule" type="System.Web.Handlers.ScriptModule,
     System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
     PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
 
This will work like magic, you dont need to change anything in you code, you can use your existing Response.Redirect
To get more indepth knowledge of ScripModule, click on the link below

18 comments:

Amar said...

Try this:
web.config change add at pages element

enableEventValidation="false"

Brij said...

Setting EnableEventValidation = False, will break all the Extenders not to work. The Extenders need to inject the script during partial postback and the PageRequestManager class will only render the controls present on a form if event validation is active. The Render of the ScriptManager class will not be called if EventValidation is set to false.

Patti said...

I couldn't find the solution to the error I was getting, and this worked beautifully. THANK YOU!!

Anonymous said...

Thank you amar. It worked perfectly!

Anonymous said...

I am getting this same error.

1] I have Added the solution2 recommended to my web.config even.

2] Also i am not using Response.Write anywhere on page.

3] This problem occurs only after a certian period of time i left the page ideal for about 25 minutes and then come back and click any link or button.

rockspider said...

I tried Solution 1 (redirectTo method) and Solution 2 (web config) and neither worked.

Any other suggestions?

rockspider said...

I found a different cause to this problem. My issue is to do with Windows Authentication and domain related.

After having eliminated issues with RegisterStartupScript (solution 1) and ScriptModule (solution2), I read on another post that Response.Write could cause this problem so I removed all response.write commands. I also read that buttons inside updatepanels could cause partial postbacks so I added triggers as required.
After eliminating all these issues I was still getting this problem.

I get the error after clicking on a button that pops up another browser window. In order to popup a new window I use the ScriptManager.RegisterStartupScript method in the button-click event in the codebehind:

protected void myLinkButton_Click(object o, EventArgs e)
{
string externalUrl = “http://myexternalurl.com/login”;
string url = "window.open('" + externalUrl + "','','scrollbars=yes,menubar=yes,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "pop", url, true);
}

The problem: the button loads an external URL into the window, and the externalServer is set up with Windows Authentication. Once the user logs into the popup window, the user’s htppcontext is changed, and also the context of the original IE window (supposedly a security feature in IE). The result being that the original browser window’s context is invalid and hence the “Sys.WebForms. PageRequestManagerParserErrorException” is displayed as if an invalid postback was encountered.

Solution: I have not yet implemented a solution but I am going to try either of the following:
- to either provide the external url as a non-clickable piece of text or a label (forcing the user to copy the url into a new browser),
- change the external server’s authentication type (to forms), or
- to move the external app into my domain (if at all possible),
- I suppose you could also look into opening a new browser instance (i.e. using the system.diagnostics.process.start command as opposed to a javascript popup), but this comes with its own security issues, so I am not sure how it will work out.
- Any other ideas?

Ian said...

Thank you so very very much for this solution. Added to web.config and all my problems have gone away. Thanks again..!

arun k said...

Second solution is like a magic
It works really...
I fuckd that error by adding key to the web config file

Suresh Raj said...

Thanks a Lot "solution2 Recommended" works for me
am using Response.Redirect
inside an Textchanged Event of my Textbox which is inside the updatePanel causes the Error

ASP .Net Programmer said...

Hi
It is a good codding.
Thank for sharing.

Anonymous said...

Hola! Laurie Whitis . payday loans

Anonymous said...

payday loans toronto This website is one of the best I enjoyed it hugely
AAA Toronto Payday Loans 1172 Bay St #101, Toronto, ON M5S 2B4 (416) 477-2817

David said...

You made my day!
Thanks

Anonymous said...

Thank you so much!
I lost almost half day trying to solve this problem!

Anonymous said...

you have to add Gridview to the postback trigger, not the link button alone...

Anonymous said...

Thank you!!!

Ajay said...

Solution2 worked for me however I replace the code in httpmodule it wroked out, thx dude.
Ganesh Bhosale

Post a Comment