Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

For Beginners: In ASP.NET how to show a Javascript Alert message box?

Posted by Paul on December 2, 2010


I have seen frequently that beginners in ASP.NET try to use Windows Forms MessageBox and fails.

I would like to say that MessageBox class is meant for Windows Forms Application and not ASP.NET Web applications.

In ASP.NET applications, you have to:

Use either a page to show the message

or

Use javascript alert function to show the message (the example is given below)

Example

Create a new web application using File > New Project > Web > ASP.NET Web Application

image

In the solution Explorer, right click on the Default.aspx and use View Code command.

Replace the Page_Load method code as below:

protected void Page_Load(object sender, EventArgs e)

{

    string message = "Test Message Box in ASP.NET";

    string script = "<script language=’javascript’>alert(‘" + message + "’);</script>";

    ClientScript.RegisterStartupScript(GetType(), "key", script);

}

Now you can execute the application.

Output

On loading the page you will see the following message box (if not enable javascript in your browser)

image

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s