在Web应用程序中显示消息框 [英] show message box in web application

查看:122
本文介绍了在Web应用程序中显示消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i在我的网络应用程序中有一个要求,如果我输入文本框中的值并按下添加按钮,则值应添加到网格中。

但如果我将相同的值添加到已经在网格中的文本框中并点击添加按钮,则应显示javascript警告..



i写了逻辑来检查值,如果已经存在于网格中....但是在提示javascript警报时出现问题...



请建议



我的代码就像





i had a requirement in my web application where if i eneter value in to textbox and hit add button the values should add in to the grid..
but if i add the same value in to textbox which is already in grid and hit add button the javascript alert should display..

i wrote the logic for checking the value if already existing in grid....but having problem in diaplying the javascript alert...

please suggest

my code is like

 private void ShowMessageBox(string message)//method for showing alert box in grid when the same values are added
    {
        if (!string.IsNullOrEmpty(message))
        {
            if (message.EndsWith("."))
                message = message.Substring(0, message.Length - 1);
        }

        System.Text.StringBuilder sbScript = new System.Text.StringBuilder(500);
sbScript.Append("<script type='text/javascript'>" + Environment.NewLine);
                message = message.Replace("\n", "\\n").Replace("\"", "'");
        sbScript.Append(@"alert( """ + message + @""" );");
        sbScript.Append(@"</script>");







Page currentPage = HttpContext.Current.CurrentHandler as Page;
                if (currentPage != null && !currentPage.ClientScript.IsStartupScriptRegistered("ShowMessageBox"))
        {
                        currentPage.ClientScript.RegisterStartupScript(typeof(Page), "ShowMessageBox", sbScript.ToString());
        }
        //typeof(Alert)
    }

推荐答案

嗨试试这个..



页面currentPage = HttpContext.Current.CurrentHandler as Page;





Hi try this..

Page currentPage = HttpContext.Current.CurrentHandler as Page;


if (this.Page != null && !this.Page.ClientScript.IsStartupScriptRegistered("ShowMessageBox"))
            {
                this.Page.ClientScript.RegisterStartupScript(typeof(Page), "ShowMessageBox", sbScript.ToString());
            }


Page.ClientScript.RegisterStartupScript(this.GetType(), "showMyMessage", "ShowMessage('Successfully saved!');", true);


你好,我遇到过一个类文件,改了一下为我工作,这个方法是非常容易,无处不在,IE,Chrome和Firefox其他我还没有测试过。



只需将此类文件添加到您的项目中



Hello, I have came across a Class file, changed it a bit to work for me, This method is very easy and works everywhere, IE, Chrome and Firefox others I have not tested.

Just add this Class File to Your Project

using System;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MessageAnkit
{
    
    public class MessageBox
    {

       private static Hashtable m_executingPages = new Hashtable();

       
        public static void Show(string sMessage)
        {
           
            if (!m_executingPages.Contains(HttpContext.Current.Handler))
            {
               
                Page executingPage = HttpContext.Current.Handler as Page;
                
                if (executingPage != null)
                {
                    Queue messageQueue = new Queue();
                    messageQueue.Enqueue(sMessage);
                    m_executingPages.Add(HttpContext.Current.Handler, messageQueue);
                    executingPage.Unload += new EventHandler(ExecutingPage_Unload);
                }
            }
            else
            {
                Queue queue = (Queue)m_executingPages[HttpContext.Current.Handler];
                queue.Enqueue(sMessage);
            }
        }

    
        private static void ExecutingPage_Unload(object sender, EventArgs e)
        {
         
            Queue queue = (Queue)m_executingPages[HttpContext.Current.Handler];

            if (queue != null)
            {
                StringBuilder sb = new StringBuilder();
                int iMsgCount = queue.Count;
                sb.Append("<script language='javascript'>");
                string sMsg;
                while (iMsgCount-- > 0)
                {
                    sMsg = (string)queue.Dequeue();
                    sMsg = sMsg.Replace("\n", "\\n");
                    sMsg = sMsg.Replace("\"", "'");
                    sb.Append(@"alert( """ + sMsg + @""" );");
                }

                sb.Append(@"</" + "script>");
                m_executingPages.Remove(HttpContext.Current.Handler);
                HttpContext.Current.Response.Write(sb.ToString());
            }
        }

    }
}







之后只需添加此行即可显示消息






After that Just Add this line to display a Message

MessageAnkit.MessageBox.Show("Message String goes here");







我想这有帮助。



快乐编码...... :)



Ankit Roy




I think this helps.

Happy Coding ... :)

Ankit Roy


这篇关于在Web应用程序中显示消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆