How to show server side messagebox when ajax is using,Messagebox in Ajax
When Ajax Content is using ,it is too tough to show a javascript alert message box from the server side in the webpage.Following function to show message box in Ajax update panel.
Step 1.
Create a Class file name named as WebMsgBox.cs
Step 2.
Copy the follwoing code in that cs file
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());
}
}
static public void DisplayAJAXMessage(Control page, string msg)
{
string myScript = String.Format("alert('"+ msg +"');",msg);
//string myScript = String.Format("alert('"{0}"');",msg);
ScriptManager.RegisterStartupScript(page, page.GetType(),
"MyScript", myScript, true);
}
}
}
Step 3.
Now call the method show from the CS file of your webpage
TestWebMsgApp.WebMsgBox.Show("Hello");
Now you can see the server side message box in the web page when ajax is using
Search This Blog
Subscribe to:
Post Comments (Atom)
1 comment:
Its good. I used this code. It's very nice
Post a Comment