Introduction: In this article i
am going to share the way to create a class to show the message box in asp.net
website as we use in window applications.
In previous articles i explained Ajax ModalPopupExtender example to open login form in popup window in asp.net and Message box in asp.net website using JavaScript and Show message box and redirect to another page/website in using JavaScript and Open and close pop up window in asp.net using javascript and Open Pop up window on Drop down selection in Asp.net.
Description: While developing web
application we need to show message box frequently. For example to show the
Message like "Record saved successfully", "Record could not be
saved" or "Record deleted
successfully" etc. But in web forms there is no inbuilt feature to show
message box. So we need to create our own custom class to pop up message box.
Implementation: Let's create a
demo website to demonstrate the messagebox implementation in action.
- Launch Visual studio - >File Menu -> New -> website -> Name it "MessageBoxDemo" or anything as per your requirement.
- Right click on the MessageBoxDemo project in solution explorer -> Click Add New Item - > select class and name it "MessageBox" as shown in image below:
Click on image to enlarge |
- A window will pop up as shown in image below.
Click on image to enlarge |
- Click yes. It will create App_Code folder in the root of the project and also add the new class "MessageBox" in the App_Code folder automatically.
- Open MessageBox.cs class and write the below code in it.
The complete code in
MessageBox.cs file is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Web.UI;
using System.Text;
/// <summary>
/// Summary description for MessageBox
/// </summary>
public class MessageBox
{
private static Hashtable m_executingPages = new Hashtable();
private MessageBox()
{
}
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
+= 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 = null;
while (System.Math.Max(System.Threading.Interlocked.Decrement(ref
iMsgCount), iMsgCount + 1) > 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());
}
}
}
Congrats!! Your message box class is ready. To
show the message box you need to call the show() method of the MessageBox class
and pass string message as demonstrated in the example below. You can call it
as many times as required in your application.
- Now add a new page "default.aspx" in the project and place a button on the page as:
<asp:Button ID="btnMsgBox"
runat="server"
Text="Show
MessageBox"
onclick="btnMsgBox_Click"
/>
ASP.NET Code
- In the code behind (default.aspx.cs) write the code as:
protected void
btnMsgBox_Click(object sender, EventArgs e)
{
MessageBox.Show("This
is Message Box Demonstration");
}
Note : You can call the show method of the MessageBox class in the asp.net page with vb language i.e. you can use same messagebox in the asp.net vb page.
Now over to you:
" I hope you have got the trick to create custom message box in Asp.Net and
If you like my work; you can appreciate by leaving your comments, hitting
Facebook like button, following on Google+, Twitter, Linked in and Pinterest,
stumbling my posts on stumble upon and subscribing for receiving free updates
directly to your inbox . Stay tuned and stay connected for more technical
updates."
3 comments
Click here for commentsNice One Sir ji
ReplyI am glad you found it helpful..stay connected and keep reading for more useful updates like this...:)
Reply'ASP.net MessageBox
Reply'Add a scriptmanager to the ASP.Net Page
try:
Dim sMsg As String = "My Message"
ScriptManager.RegisterStartupScript(Page, Page.GetType, Guid.NewGuid().ToString(), "alert('" & sMsg & "')", True)
If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..