Thursday, June 26, 2008

How to have a pop out dialog box in ASP.NET

JHaIn all good web applications, the user is asked to confirm whether he/she wants to delete something in case the delete button was pressed accidentally.

Although it seems like a pain to do, it is actually really easy if you find it acceptable to use javascript's “confirm” statement, which will popup a dialog box with a particular question with “ok” and “cancel” buttons. You have no control of the title of the popup, but in IE it says “Microsoft Internet Explorer“ and I believe it says “[Javascript Application]“ or similar in Firebird.





The javascript code for it is simple:


function confirm_delete()
{
if (confirm("Are you sure you want to delete the custom search?")==true)
return true;
else
return false;
}


Using code-behind, you can attach the javascript popup dialog to the button:


_myButton.Attributes.Add("onclick", "return confirm_delete();");

When you click on the delete button, the javascript popup dialog asks if you want to delete the search. If you choose “cancel“, your “DeleteSearch_Click” event is never fired. If you choose “ok”, the event is fired and you can delete the item.

No comments:

Post a Comment