How to display message boxes in server-side code?
4 answers
Add the WebMsgBox class below to your project. Then wherever you need to pop up a message box from code, just do this:
WebMsgBox.Show("How to show alert message!");
WebMsgBox class:
using System;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.UI;
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());
}
}
}
+2
source to share
you can use ClientScript to execute javascript code from server side. To provide an alert, you need to write ClientScript.RegisterStartupScript / ClientScript.RegisterClientScriptBlock after saving the value.
If your button is in the refresh bar, you should use ScriptManager.RegisterClientScriptBlock instead of ClientScript.
0
source to share
I found this link might be helpful: http://www.aspsnippets.com/Articles/Show-Alert-Message-in-ASPNet-from-Server-Side-using-C-and-VBNet.aspx
0
source to share