Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

web message box asp.net c# or how to display a message box in asp.net web application using c#

The following code shows how to create a little complicate MessageBox that it can make a choice and access web methods.

using TestWebMsgApp; // add this to name spaces


 protected void btnSignin_Click(object sender, ImageClickEventArgs e)
    {

 WebMsgBox.Show("This email id already exists");
 
 }

Add this class file  WebMsgBox .cs


using System;
using Microsoft.VisualBasic;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;



namespace TestWebMsgApp
{
public class WebMsgBox
{
protected static Hashtable handlerPages = new Hashtable();
private WebMsgBox()
{
}

public static void Show(string Message)
{
if (!(handlerPages.Contains(HttpContext.Current.Handler)))
{
Page currentPage = (Page)HttpContext.Current.Handler;
if (!((currentPage == null)))
{
Queue messageQueue = new Queue();
messageQueue.Enqueue(Message);
handlerPages.Add(HttpContext.Current.Handler, messageQueue);
currentPage.Unload += new EventHandler(CurrentPageUnload);
}
}
else
{
Queue queue = ((Queue)(handlerPages[HttpContext.Current.Handler]));
queue.Enqueue(Message);
}
}

private static void CurrentPageUnload(object sender, EventArgs e)
{
Queue queue = ((Queue)(handlerPages[HttpContext.Current.Handler]));
if (queue != null)
{
StringBuilder builder = new StringBuilder();
int iMsgCount = queue.Count;
builder.Append("<script language='javascript'>");
string sMsg;
while ((iMsgCount > 0))
{
iMsgCount = iMsgCount - 1;
sMsg = System.Convert.ToString(queue.Dequeue());
sMsg = sMsg.Replace("\"", "'");
builder.Append("alert( \"" + sMsg + "\" );");
}
builder.Append("</script>");
handlerPages.Remove(HttpContext.Current.Handler);
HttpContext.Current.Response.Write(builder.ToString());
}
}
}

}

=================================================================

second method :

protected void btnInvokeMessageBox_Click(object sender, EventArgs e) 

    MessageBox messageBox = new MessageBox(); 
    messageBox.MessageTitle = "Information"
    messageBox.MessageText = "Hello everyone, I am an Asp.net MessageBox. Please put your message here and click the following button to close me."
    Literal1.Text = messageBox.Show(this); 

Third method :
 ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + your message + "');", true);



This post first appeared on Asp.netSourceCodes, please read the originial post: here

Share the post

web message box asp.net c# or how to display a message box in asp.net web application using c#

×

Subscribe to Asp.netsourcecodes

Get updates delivered right to your inbox!

Thank you for your subscription

×