显示警报框在asp.net从code后面的(并不总是)

编程入门 行业动态 更新时间:2024-10-11 23:18:13
本文介绍了显示警报框在asp从code后面的(并不总是)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在我的asp应用程序显示一个消息框,一个通用的报警功能:

I have a generic Alert function that display a message box in my asp app:

public void Alert(string sTitle, string sMessage) { StringBuilder sbScript = new StringBuilder(); sbScript.Append("<script language='Javascript'>"); sbScript.Append("var varDateNow = new Date();"); sbScript.Append("var varTimeNow = varDateNow.getTime();"); //sbScript.Append("var varAlertTime = document.getElementById('Master_cphAlertTime').value;"); sbScript.Append("var varAlertTime = document.getElementById('cphAlertTime').value;"); sbScript.Append("if(varTimeNow - varAlertTime < 1500)"); sbScript.Append("{alert('"); sbScript.Append(strMessage); sbScript.Append("');}"); sbScript.Append("</script>"); ClientScript.RegisterStartupScript(this.GetType(), strTitle, sbScript.ToString()); }

警告框不会出现每次。什么是困惑我是为什么它会出现一段时间,并没有出现在其他时间?当它不出现次是当一个页面就要被重定向(或Server.Transfer的)到另一个页面。

The Alert box does not appear every time. What is confusing me is why does it appear sometime and not appear at other times? The times when it does not appear is when a page is about to get redirected (or server.transfer) to another page.

任何想法,为什么随机功能?

Any ideas why the random functionality?

推荐答案

您可以在应用程序中实现这个静态类

you can implement this static class in your application

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 += new EventHandler(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; while (iMsgCount-- > 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()); } }

和调用像这样只是一个简单的通话阿勒特/消息框 MessageBox.Show(你好);

and the call the allert/message box with just a simple call like this MessageBox.Show("hello");

更多推荐

显示警报框在asp.net从code后面的(并不总是)

本文发布于:2023-11-27 16:07:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638684.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   asp   code   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!