Asp.Net Alert Box

 

Introduction

Today i will describe almost everthing about Asp.Net alert box or message box

What actually is a alert box :

Alert box is a small box that appears on the display screen to give you information or to warn you about a potentially damaging operation. For example, it might warn you that the system is deleting one or more files. Unlike dialog boxes, alert boxes do not require any userinput. However, you need to acknowledge the alert box by pressing the Enter key or clicking a mouse button to make it go away.

Today or in future you want to show alert boxes in asp.net applications in many scenarios.

Let me discuss some of the scenarios :

1. Suppose, If any error occurs in the page and you want to display that error in a alert box.

2. Want to display confirmation alert box before deleting or updating records.

3. Display validation message in the alert boxes.

You can display alert box into asp.net using javascipt or jquery. You can display it using any of the following code:

Response.Write("<script>alert('This is information alert box. And you can call it anywhere.')</script>");

or

Page.ClientScript.RegisterStartupScript(this, GetType(),"ALERT","alert('Selected items are removed successfully')",  true);

Example.

I will show you very common example of asp.net alert box. These often used when you are going to delete records. These ask you for the confirmation of deletion of the records. If you press YES it will delete the records and if you press NO it will not delete any records.

Step 1.

Drag and drop a Asp.Net Button control on the Web Form for deleting the records and give it ID as "Delete_Button".

Step 2.

Double click on it and write your logic for deleteing records on click event of the Button.

Step 3.

Write the below code on the load event of the Form.

if (!Page.IsPostBack)
{
   Delete_Button.Attributes.Add("onclick","return confirm('Are you sure you want to delete the Record(s) ?');");
}

It will add the Attributes to the Button when form is loaded for the first time.

You are Done. Now Run this example.

You will see when you click on the delete button. It will ask you for the confirmation, if you press "YES" then it will delete the records otherwise not.

Enjoy and Keep Coding.

 

 

Give your Valuable Comments.
comments powered by Disqus
 
Follow me on twitter
About Me
Harpreet Singh
I began programming with C++ when i was 17. Then at the middle of my study .NET came. Then I began to read C# and VB.NET. By the time i learnt SQL and ASP.NET, and developed some websites such as news portals that are active now. Currently i am running my own Software company.
More Articles