Click here to Skip to main content
15,891,774 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
hi,

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>");


C#
Page currentPage = HttpContext.Current.CurrentHandler as Page;
                if (currentPage != null && !currentPage.ClientScript.IsStartupScriptRegistered("ShowMessageBox"))
        {
                        currentPage.ClientScript.RegisterStartupScript(typeof(Page), "ShowMessageBox", sbScript.ToString());
        }
        //typeof(Alert)
    }
Posted
Updated 2-Jan-14 20:12pm
v3

Hi try this..

Page currentPage = HttpContext.Current.CurrentHandler as Page;


C#
if (this.Page != null && !this.Page.ClientScript.IsStartupScriptRegistered("ShowMessageBox"))
            {
                this.Page.ClientScript.RegisterStartupScript(typeof(Page), "ShowMessageBox", sbScript.ToString());
            }
 
Share this answer
 
Page.ClientScript.RegisterStartupScript(this.GetType(), "showMyMessage", "ShowMessage('Successfully saved!');", true);
 
Share this answer
 
v2
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");



I think this helps.

Happy Coding ... :)

Ankit Roy
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900