1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Building a Message Box Class in ASP.NET 2.0

Discussion in 'ASP.NET' started by naimish, Sep 7, 2009.

  1. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth

    Introduction



    Quite Often, We may wish to inform the user of a successful or an unsuccessful operation in a web form application. An effective way to communicate an important message to the user is through the use a MessageBox or a JavaScript "alert".

    The MessageBox class in the System.Windows.Forms namespace is usable only from Windows Forms and NOT ASP.NET Web Forms. In order to alert the user we need to add some client side code into the HTML page. This is a simple task but can become quite annoying if this functionality is required on a regular basis.


    Background



    Hence, It would be nice if we could simply call a static method from any page that would deal with the client side JavaScript required to display the alert's.

    This reusable asset explains about a small MessageBox class with a static Show();
    method. This Class can be included in the Project and can be used by simply calling the Show method for displaying a Message Box.

    The code



    Code: ASP.NET

    Code:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Text;
    namespace Message
    {
    	/// <summary>
    	/// Summary description for MessageBox.
    	/// </summary>
    	public class MessageBox
    	{
    		private static Hashtable m_executingPages = new Hashtable();
    		public MessageBox()
    		{
    			//
    			// TODO: Add constructor logic here
    			//
    		}
    		public static void 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.Handler as Page;
    				if( executingPage != null )
    				{
    					// Create a Queue to hold one or more messages.
    					Queue messageQueue = new Queue();
    					// 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 += new EventHandler( 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
    		private static void 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 = new StringBuilder();
    				// 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( @"alert( """ + sMsg + @""" );" );
    				}
    				// Close our JS
    				sb.Append( @"</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() );
    			}
    		}
    	}
    }



    Thanks :)

     
    blackrubybarb likes this.
  2. jimh

    jimh New Member

    Joined:
    Sep 16, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    [FONT=&quot]Hi,

    Thanks for allowing me to be a part of this forum. I look forward to becoming an active member.

    Jim


    [/FONT]
     
  3. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Thanks to shabbir buddy, He's the one :D
     
  4. divengrabber

    divengrabber New Member

    Joined:
    Aug 9, 2009
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    thanks for saving my time. I wast lots of time while doing this.
     
  5. michaelsm

    michaelsm New Member

    Joined:
    Aug 28, 2009
    Messages:
    11
    Likes Received:
    1
    Trophy Points:
    0
    Thankyou very much.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
  7. laxsacon

    laxsacon New Member

    Joined:
    Oct 5, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I was registered at your forum. I have printed the test message. Do not delete, please.
     
  8. rasd123

    rasd123 Banned

    Joined:
    Nov 4, 2009
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Thanks to share this information.
     
  9. joshuab

    joshuab New Member

    Joined:
    Nov 13, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi naimish..

    Thanks for your Information...
    I have been looking for this answer.
     
  10. technica

    technica New Member

    Joined:
    Dec 15, 2007
    Messages:
    107
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    good article. I am going to use this in one of our projects. Thanks for sharing it.
     
  11. jerryvn01

    jerryvn01 New Member

    Joined:
    Jun 30, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0

    Hi everybody,

    I uploaded one file. But It can not display. I used internet explorer 8.0.

    How can I repair? Or some problems happened to IE8?


    Rgs
     
  12. sijochethalan

    sijochethalan New Member

    Joined:
    Sep 14, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    my name sijo and could you please help me for studying asp.net 2005 and i need your help please.Thank you
     
  13. blackrubybarb

    blackrubybarb New Member

    Joined:
    Oct 28, 2010
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    .NET Developer
    Location:
    Sri Lanka
    Really Helpful.. thanx
     
  14. ellyka112

    ellyka112 New Member

    Joined:
    Nov 22, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Thanks to shabbir buddy, He's the one
     
  15. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    It is not my forum but forum is always of members and I am just a facilitator to keep things organized.
     

Share This Page