Hi,
I'm converting an existing site to use the AJAX toolkit. So far the only problems I've had have been the usual teething problems when learning a new technology. Now though, I'm having a problem with using one of my existing classes from code invoked from a control in an update panel. It's always worked fine until using the update panel. I've pasted the code below (i got it off the web by the way). If i call it from a control outside of an update panel i have no problems.
The error message i'm getting is as follows:
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 'leteWeek"));
});
|<script language='ja'.
Any help/pointers would be much appreciated.
publicclassMessageBox
{
privatestaticHashtable m_executingPages =newHashtable();private MessageBox(){}
publicstaticvoid Show(string sMessage ){
// If this is the first time a page has called this method then
if( !m_executingPages.Contains(HttpContext.Current.Handler ) ){
// Attempt to cast HttpHandler as a Page.
Page executingPage =HttpContext.Current.HandlerasPage;if( executingPage !=null ){
// Create a Queue to hold one or more messages.
Queue messageQueue =newQueue();// Add our message to the Queue
messageQueue.Enqueue( sMessage );
// Add our message queue to the hash table. Use our page reference
// (IHttpHandler) as the key.
m_executingPages.Add(HttpContext.Current.Handler, messageQueue );// Wire up Unload event so that we can inject
// some JavaScript for the alerts.
executingPage.Unload +=newEventHandler( ExecutingPage_Unload );}
}
else
{
// If were here then the method has allready been
// called from the executing Page.
// We have allready created a message queue and stored a
// reference to it in our hastable.
Queue queue = (Queue) m_executingPages[HttpContext.Current.Handler ];// Add our message to the Queue
queue.Enqueue( sMessage );
}
}
// Our page has finished rendering so lets output the
// JavaScript to produce the alert's
privatestaticvoid ExecutingPage_Unload(object sender,EventArgs e){
// Get our message queue from the hashtable
Queue queue = (Queue) m_executingPages[HttpContext.Current.Handler ];if( queue !=null ){
StringBuilder sb =newStringBuilder();
// How many messages have been registered?
int iMsgCount = queue.Count;// Use StringBuilder to build up our client slide JavaScript.
sb.Append("<script language='javascript'>" );// Loop round registered messages
string sMsg;while( iMsgCount-- > 0 ){
sMsg = (string) queue.Dequeue();
sMsg = sMsg.Replace("\n","\\n" );sMsg = sMsg.Replace("\"","'" );
sb.Append(@dotnet.itags.org."alert( """ + sMsg +@dotnet.itags.org.""" );" );}
// Close our JS
sb.Append(@dotnet.itags.org."</script>" );// Were done, so remove our page reference from the hashtable
m_executingPages.Remove(HttpContext.Current.Handler );// Write the JavaScript to the end of the response stream.
HttpContext.Current.Response.Write( sb.ToString() );}
}
Hi,
you can't use Response.Write during a partial postback. The reason is that the response sent after a partial postback has a special format (you could useFiddler to see it). If you use Response.Write, you're directly writing to the response stream and corrupting the response's format.
To inject JavaScript during a partial postback, you should use the static RegisterXXX methods of the ScriptManager control.
Thanks for that. I found aa article elsewhere explaining how to use the script manager and I've got that working fine. Thanks for your help.
No comments:
Post a Comment