Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 880657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:08:05+00:00 2026-05-15T12:08:05+00:00

I am maintaining an ASP.NET site, and I was attempting to get the dialogs

  • 0

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() );
       }
    }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:08:05+00:00Added an answer on May 15, 2026 at 12:08 pm

    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:

    <webapp:MessageBox ID="messageBox" Callback="showMessage" runat="server" />
    <script type="text/javascript">
        function showMessage(messages) {
            $("#dialog p").empty();
            for(var msg in messages) {
                $("#dialog p").html += msg;
            }
            $("#dialog p").show();
        }
    </script>
    

    I didn’t test the callback script, but you get the idea.

    and the code:

    /// <summary>
    /// MessageBox is a class that allows a developer to enqueue messages to be
    /// displayed to the user on the client side, when the page next loads
    /// </summary>
    public class MessageBox : System.Web.UI.UserControl
    {
    
        /// <summary>
        /// queues up a message to be displayed on the next rendering.
        /// </summary>
        public static void Show( string message )
        {
            Messages.Enqueue( message );
        }
    
        /// <summary>
        /// queues up a message to be displayed on the next rendering.
        /// </summary>
        public static void Show( string message, params object[] args )
        {
            Show( string.Format( message, args ) );
        }
    
        /// <summary>
        /// override of OnPreRender to render any items in the queue as javascript
        /// </summary>
        protected override void OnPreRender( EventArgs e )
        {
            base.OnPreRender( e );
    
            if ( Messages.Count > 0 )
            {
    
                StringBuilder script = new StringBuilder();
                int count = 0;
    
                script.AppendLine( "var messages = new Array();" );
    
                while ( Messages.Count > 0 )
                {
                    string text = Messages.Dequeue();
                    text = text.Replace( "\\", "\\\\" );
                    text = text.Replace( "\'", "\\\'" );
                    text = text.Replace( "\r", "\\r" );
                    text = text.Replace( "\n", "\\n" );
    
                    script.AppendFormat( "messages[{0}] = '{1}';{2}", count++, HttpUtility.HtmlEncode(text), Environment.NewLine );
                }
    
                if ( string.IsNullOrEmpty( Callback ) )
                {
                    // display as "alert"s if callback is not specified
                    script.AppendFormat( "for(i=0;i<messages.length;i++) alert(messages[i]);{0}", Environment.NewLine );
                }
                else
                {
                    // call the callback if specified
                    script.AppendFormat( "{0}(messages);{1}", Callback, Environment.NewLine );
                }
    
                Page.ClientScript.RegisterStartupScript( this.GetType(), "messages", script.ToString(), true );
            }
        }
    
        /// <summary>
        /// gets or sets the name of the javascript method to call to display the messages
        /// </summary>
        public string Callback
        {
            get { return callback; }
            set { callback = value; }
        }
        private string callback;
    
        /// <summary>
        /// helper to expose the queue in the session
        /// </summary>
        private static Queue<string> Messages
        {
            get
            {
                Queue<string> messages = (Queue<string>)HttpContext.Current.Session[MessageQueue];
                if ( messages == null )
                {
                    messages = new Queue<string>();
                    HttpContext.Current.Session[MessageQueue] = messages;
                }
                return messages;
            }
        }
        private static string MessageQueue = "MessageQueue";
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking at an asp.net 2 web application that I am maintaining (but
I have an ASP.NET site that I am maintaining. Currently it has code that
Our server application runs as a service and has a ASP.NET MVC3 web-based frontend
I am working on maintaining a ASP.NET MVC application that has the following coding
Greetings folks, The ASP.NET application I'm maintaining has a fairly long start up procedure.
I'm maintaining an ASP.NET site where users can log on to register some set
I am developing a multilingual ASP.NET web site. I want the users to be
I have an old ASP.NET 1.1 site that I am maintaining. We are working
I'm maintaining a C# ASP.NET application and I have come across the following little
I am maintaining a SOAP web service (ASP.NET version 2.0) and I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.