9

if I need to show a MessageBox on my ASP.NET WebForm, how to do it?

I try: Messagebox.show("dd");

But it's not working.

Kris van der Mast
  • 16,343
  • 8
  • 39
  • 61
Gold
  • 60,526
  • 100
  • 215
  • 315
  • 2
    Do you want this message box to show up on the client or the server? You are aware that ASP.NET code runs on the server, right? – Gabe Nov 29 '10 at 07:49
  • Using alert('message') in Asp.net in not professional at all! It looks like a OS message you can't style it You should alwasy "create" your own modal popUp maybe using JQuery to make your life easier. Alert('') in a web context it is a bad practice – Massimiliano Peluso Nov 29 '10 at 10:23
  • Does this answer your question? [ASP.NET Web Application Message Box](https://stackoverflow.com/questions/9720143/asp-net-web-application-message-box) – TylerH Jun 29 '21 at 16:29

9 Answers9

14

MessageBox doesn't exist in ASP.NET. If you need functionality in the browser, like showing a message box, then you need to opt for javascript. ASP.NET provides you with means to inject javascript which gets rendered and executed when the html sent to the browser's loaded and displayed. You can use the following code in the Page_Load for example:

Type cstype = this.GetType();

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;

// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, "PopupScript"))
{
    String cstext = "alert('Hello World');";
    cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
}

This sample's taken from MSDN.

Kris van der Mast
  • 16,343
  • 8
  • 39
  • 61
  • I've had this in my code in my Master Page for calling by my website pages, but I get mixed results from it. Sometimes it works, and other times nothing happens. –  Nov 16 '12 at 17:26
  • Please start out a new thread with your specific findings and provide code with a reproducible path so people can actually test it locally and see what's going wrong. – Kris van der Mast Nov 19 '12 at 08:21
7

There is pretty concise and easy way:

Response.Write("<script>alert('Your text');</script>");
Vadim
  • 633
  • 1
  • 8
  • 17
3

Messagebox is for windows only. You have to use Javascript

Alert('dd'); 
peterthegreat
  • 406
  • 3
  • 9
1

You could just simply write but you have to use JavaScript regardless.

Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('dd')</script>");
0

One of the options is to use the Javascript.

Here is a quick reference where you can start from.

Javascript alert messages

Hps
  • 1,177
  • 8
  • 9
0

Message box is only defaultly available for windows form application.If you want to use the message box resource the you would have to use 'using system.windows.forms' to enable the message box for web forms mode.

0

It's true that Messagebox.show("dd"); is not a part of using System.Web;,

I felt the same situation for most of time. If you want to do this then do the following steps.

  • Right click on project in solution explorer
  • go for add reference, then choose .NET tab

  • And select, System.windows.forms (press 's' to find quickly)

u can get the namespace, now u can use Messagebox.show("dd");

But I recommend to go with javascript alert for this.

PawanS
  • 7,033
  • 14
  • 43
  • 71
0

I took the code from the brilliant @KrisVanDerMast and made it wrapped up in a static method that can be called as many times as you want on the same page!

/// <summary>
/// Shows a basic MessageBox on the passed in page
/// </summary>
/// <param name="page">The Page object to show the message on</param>
/// <param name="message">The message to show</param>
/// <returns></returns>
public static ShowMessageBox(Page page, string message)
{
    Type cstype = page.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = page.ClientScript;

    // Find the first unregistered script number
    int ScriptNumber = 0;
    bool ScriptRegistered = false;
    do
    {
        ScriptNumber++;
        ScriptRegistered = cs.IsStartupScriptRegistered(cstype, "PopupScript" + ScriptNumber);
    } while (ScriptRegistered == true);

    //Execute the new script number that we found
    cs.RegisterStartupScript(cstype, "PopupScript" + ScriptNumber, "alert('" + message + "');", true);
}
Westley Bennett
  • 554
  • 4
  • 19
-3

You may use MessageBox if you want but it is recommended to use alert (from JavaScript) instead.

If you want to use it you should write:

System.Windows.Forms.MessageBox.Show("Test");   

Note that you must specify the namespace.

Kypros
  • 2,997
  • 5
  • 21
  • 27
Mile Laszlo
  • 137
  • 2
  • 9