I am maintaining an ASP.NET site, and I was attempting to get the dialogs looking better using jQuery. The web application has a C# class called MessageBox which allows messages to be shown to the client from the server side…. essentially in the C# on an aspx codebehind if some logic ‘does not compute’, you can just MessageBox.Show(‘your error message’);
Since the MessageBox class appeared to just ‘inject’ javascript…the “alert(your message)” I tried changing the javascript to a jquery dialog call:
html: the standard jQuery example dialog… (cut off the tags on purpose…just to get the code example to show up… there is probably a real way to do this on here… but this is my first post…)
div id="dialog" title="Example dialog"> p>Some text that you want to display to the user./p> /div>
jQuery:
I commented out the Alert, and substituted:
sb.Append(“$(‘dialog’).dialog(‘open’);”);
while( iMsgCount-- > 0 )
{
sMsg = (string) queue.Dequeue();
sMsg = sMsg.Replace( "\n", "\\n" );
sMsg = sMsg.Replace( "\"", "'" );
//sb.Append( @"alert( """ + sMsg + @""" );" );
**** sb.Append("$('dialog').dialog('open');"); ****
}
I was expecting this to open the dialog set up in html, however nothing shows.
I figured javascript is javascript… and that executing instead a jQuery call versus manual Alert wouldn’t matter… however clearly there is a disconnect.
Any thoughts on how to solve this problem? Or any better implementations out there I am not aware of?
Thanks, for any and all help… I’ve include the full MessageBox class below.
Curt.
public class MessageBox
{
private static Hashtable m_executingPages = new Hashtable();
private MessageBox(){}
public static void Show( string sMessage )
{
if( !m_executingPages.Contains( HttpContext.Current.Handler ) )
{
Page executingPage = HttpContext.Current.Handler as Page;
if( executingPage != null )
{
Queue messageQueue = new Queue();
messageQueue.Enqueue( sMessage );
m_executingPages.Add( HttpContext.Current.Handler, messageQueue );
executingPage.Unload += new EventHandler( ExecutingPage_Unload );
}
}
else
{
Queue queue = (Queue) m_executingPages[ HttpContext.Current.Handler ];
queue.Enqueue( sMessage );
}
}
private static void ExecutingPage_Unload(object sender, EventArgs e)
{
Queue queue = (Queue) m_executingPages[ HttpContext.Current.Handler ];
if( queue != null )
{
StringBuilder sb = new StringBuilder();
int iMsgCount = queue.Count;
sb.Append( "" );
string sMsg;
while( iMsgCount-- > 0 )
{
sMsg = (string) queue.Dequeue();
sMsg = sMsg.Replace( "\n", "\\n" );
sMsg = sMsg.Replace( "\"", "'" );
sb.Append( @"alert( """ + sMsg + @""" );" );
}
sb.Append( @"" );
m_executingPages.Remove( HttpContext.Current.Handler );
HttpContext.Current.Response.Write( sb.ToString() );
}
}
}
this is bizarre… I wrote a class almost identical a long time ago. for a second I thought you were using it!
anyway, I dug up the code from mine. I’ve used it quite a bit. It allows you to specify a “Callback” function name in case you want to not use the “alert” functionality.
btw, you need to be careful with the static Hashtable. if you have multiple people using the app at the same time, they might get each other’s messages.
usage:
I didn’t test the callback script, but you get the idea.
and the code: