CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to Show MessageBox in ASP.Net ?
Submitted By Bala Murugan
On 3/3/2010 9:10:07 AM
Tags: ASP.Net,CodeDigest  

How to Show MessageBox in ASP.Net ?

 

We can show Message Box in Winforms application through MessageBox.Show() method like below,
MessageBox.Show("You must enter a name.", "Name Entry Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

 

Since, the above functions can be used only in winforms we have to rely upon javascript to show message box in ASP.Net. Basically, to show a message box or alert box we need to inject the javascript code to the html output.

 

To do this, we can use RegisterClientScriptBlock() or RegisterStartupScript() methods of ClientScriptManager class.


Refer the below code,

RegisterClientScriptBlock()
ClientScriptManager script = Page.ClientScript;
        if (!script.IsClientScriptBlockRegistered(this.GetType(), "Alert"))
        {
            script.RegisterClientScriptBlock(this.GetType(), "Alert", "<script type=text/javascript>alert('hi')</script>");
      }

 

RegisterStartupScript()
ClientScriptManager script = Page.ClientScript;
txtName.Text = "Welcome to ASP.Net!!";
if (!script.IsStartupScriptRegistered (this.GetType(), "Alert"))
        {
            script.RegisterStartupScript (this.GetType(), "Alert", "<script type=text/javascript>alert('hi')</script>");
        }

 

The difference between both the methods are, we cannot access the page control using RegisterClientScriptBlock() method while in former we can.
Refer below,

 

ClientScriptManager script = Page.ClientScript;
txtName.Text = "Satheesh Babu";
if (!script.IsStartupScriptRegistered (this.GetType(), "Alert"))
        {
            script.RegisterStartupScript (this.GetType(), "Alert", "<script type=text/javascript>alert(document.getElementById('txtName').value);</script>");
        }

 

Read the below article to know more about ClientScriptManager class,
Multiple Ways to Call Javascript Function from CodeBehind in ASP.Net

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..