how to implement message box in web application using c#

register a script for an "Alert":

string str = "function () { alert('OMG ITS me'); }";

Page.RegisterClientScriptBlock("nameofscript", str);

you have said javascript alert message, but i need server side message using c# code to populate the message box.

Use the below mehtod

public static void ShowMessageBox(string message)
        {
            if (!string.IsNullOrEmpty(message))
            {
                if (message.EndsWith("."))
                    message = message.Substring(0, message.Length - 1);
            }

            StringBuilder sbScript = new StringBuilder(50);
            //Java Script header
            sbScript.Append("<script type='text/javascript'>" + Environment.NewLine);
            sbScript.Append("// Show messagebox" + Environment.NewLine);
            message = message.Replace("\n", "\\n").Replace("\"", "'");
            sbScript.Append(@"alert( """ + message + @""" );");
            sbScript.Append(@"</script>");           
            // Gets the executing web page
            Page currentPage = HttpContext.Current.CurrentHandler as Page;
            // Checks if the handler is a Page and that the script isn't already on the Page
            if (currentPage != null && !currentPage.ClientScript.IsStartupScriptRegistered("ShowMessageBox"))
            {
                currentPage.ClientScript.RegisterStartupScript(typeof(Alert),"ShowMessageBox", sbScript.ToString());
            }
        }

if you want to give message box without script than you can insert the snippet of mbox..

how do i use the snippet without the script..
plz. urgent requirement...

Use the below mehtod

public static void ShowMessageBox(string message)
        {
            if (!string.IsNullOrEmpty(message))
            {
                if (message.EndsWith("."))
                    message = message.Substring(0, message.Length - 1);
            }

            StringBuilder sbScript = new StringBuilder(50);
            //Java Script header
            sbScript.Append("<script type='text/javascript'>" + Environment.NewLine);
            sbScript.Append("// Show messagebox" + Environment.NewLine);
            message = message.Replace("\n", "\\n").Replace("\"", "'");
            sbScript.Append(@"alert( """ + message + @""" );");
            sbScript.Append(@"</script>");           
            // Gets the executing web page
            Page currentPage = HttpContext.Current.CurrentHandler as Page;
            // Checks if the handler is a Page and that the script isn't already on the Page
            if (currentPage != null && !currentPage.ClientScript.IsStartupScriptRegistered("ShowMessageBox"))
            {
                currentPage.ClientScript.RegisterStartupScript(typeof(Alert),"ShowMessageBox", sbScript.ToString());
            }
        }

Itz giving me this error....

Error 1 The type or namespace name 'Alert' could not be found (are you missing a using directive or an assembly reference?)

plz. help urgent...
thanx

I know that this thread might be totally dead but here nothing. I you are doing any server side data manipulation use the OnClick event pointing to you c# code and for your message box just do an OnClientClick event pointing to a javascript function.

Ex.
Button:

<asp:Button ID="btnButton" runat="server" OnClick="btnButton_Click" OnClientClick="javascript:aMessage();" />

C#:

public void btnButton_Click(object sender, EventArgs e)
{
      //you code here
}

Javascript:

function aMessage()
{
      window.alert("Your Message Here");
}

This worked fine with my app.

commented: ya but this window name does not exit in the context as i am using vs 2008 +0

I know that this thread might be totally dead but here nothing. I you are doing any server side data manipulation use the OnClick event pointing to you c# code and for your message box just do an OnClientClick event pointing to a javascript function.

Ex.
Button:

<asp:Button ID="btnButton" runat="server" OnClick="btnButton_Click" OnClientClick="javascript:aMessage();" />

C#:

public void btnButton_Click(object sender, EventArgs e)
{
      //you code here
}

Javascript:

function aMessage()
{
      window.alert("Your Message Here");
}

This worked fine with my app.

ya but this window name does not exit in the context as i am using vs 2008

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.