gpt4 book ai didi

c# - 如何从服务器端代码显示消息框?

转载 作者:行者123 更新时间:2023-11-30 22:48:39 25 4
gpt4 key购买 nike

我在 ASP.NET C# VS05 上工作。我的表单有一个按钮。在此按钮事件中,我将值保存在数据库中。我希望在保存该值后用户收到一条警告消息。

如何显示警告消息?

最佳答案

添加WebMsgBox下面的类到你的项目。然后,无论您需要从代码中弹出一个消息框,只需执行以下操作:

WebMsgBox.Show("How to show alert message!");

WebMsgBox 类:

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());
}
}
}

关于c# - 如何从服务器端代码显示消息框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1581546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com