views:

86

answers:

5

Using C#

How to display a message box in C#

I cannot find the message box in the dropdownlist....

How to display a message box in webpages...

+1  A: 

Have a read of this or simply google it and there are hundreds of examples

w69rdy
+2  A: 

In winforms use MessageBox.Show("Your message");

+1  A: 

Simply put there is no MessageBox Class in ASP.NET. Since ASP.NET executes on the server it would display on the server and not the clients machine (if there were one). Your best bet would be to use JavaScript or write your own.

Here's an example of creating your own MessageBox class for ASP.NET

public static class MessageBox
{
    //StringBuilder to hold our client-side script
    private static StringBuilder builder;

    public static void Show(string message)
    {
        //initialize our StringBuilder
        builder = new StringBuilder();

        //format script by replacing characters with JavaScript compliant characters
        message = message.Replace("\n", "\\n");
        message = message.Replace("\"", "'");

        //create our client-side script
        builder.Append("<script language=\"");
        builder.Append("javascript\"");
        builder.Append("type=\"text/javascript\">");
        builder.AppendFormat("\t\t");
        builder.Append("alert( \"" + message + "\" );");
        builder.Append(@"</script>");

        //retrieve calling page
        Page page = HttpContext.Current.Handler as Page;

        //add client-side script to end of current response
        page.Unload += new EventHandler(page_Unload);
    }

    private static void page_Unload(object sender, EventArgs e)
    {
        //write our script to the page at the end of the current response
        HttpContext.Current.Response.Write(builder);
    }
}
PsychoCoder
+1  A: 

In a webpage, two ways to show a messagebox are to use a javascript alert call or an AJAX (i.e. ASP.NET AJAX Control Toolkit) ModalPopupExtender . The former is generally simpler and easier, but you won't have much control over it or be able to support proper interactivity.

Brian
+1  A: 

Take a look at the jQuery UI Dialog widget.

Wallace Breza