How to show Alert and Confirmation Message box in ASP.NET

In this article, beginners can learn how to display a Message Box in Asp.Net using JavaScript.

1. Alert Message Box

This type of Message Box is used to give alert information to the user. This can be done using the "alert" method. Use the alert method in <script> </script> tags by using a function with a method name. Call this function on client click event in a Button as follows in the following code.

e.g.: alert("write Message here")

Image of Alert Message Box:

  1. <head runat="server">  
  2. <title>Untitled Page</title>  
  3. <script type="text/Javascript" language ="javascript" >  
  4. function alert_meth()  
  5. {  
  6. alert("Client Side MessageBox");  
  7. }  
  8. </script>  
  9. </head>  
  10. <body>  
  11. <form id="form1" runat="server">  
  12. <div>  
  13. <asp:Button ID="Button1" runat="server" Style="z-index: 100; left: 64px; position: absolute;  
  14. top: 88px" Text="Alert" OnClientClick ="alert_meth()" Font-Bold="True" ForeColor="Red" Width="72px"/>
2. Confirmation Message Box

This type of Message Box is used to give a warning to the user. Suppose user is deleting records; at that time this type of message box is used to give confirmation about the action to be taken or not by showing two Buttons as "YES'" and "NO". If the user clicks on the "YES" Button it returns Boolean "True" and for "NO" button it returns Boolean "False".



  1. <head runat="server">  
  2. <title>Untitled Page</title>  
  3. <script type="text/Javascript" language ="javascript" >  
  4. function confirm_meth()  
  5. {  
  6.   if( confirm("Do you want to continue!Click 'YES'")==true)  
  7.   {  
  8.         document.writeln ("<b>You had click on 'YES' Button</b>");  
  9.    }  
  10.   else  
  11.   {  
  12.        document.writeln ("<b>You had clic on 'NO' Button</b>");  
  13.   }  
  14. }  
  15. </script>  
  16. </head>  
  17. <body>  
  18. <form id="form1" runat="server">  
  19. <div>  
  20. <asp:Button ID="btnconfirm" runat="server" Font-Bold="True" ForeColor="Red" Style="z-index: 101;  
  21. left: 272px; position: absolute; top: 208px" Text="Confrimation MsgBox"  
  22. OnClientClick =" return confirm_meth()" Width="160px" />

Recommended Ebook

Diving Into ASP.NET WebAPI

Download Now!
Similar Articles