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

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.

Edited 6 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

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

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

This article has been dead for over six months. Start a new discussion instead.