I previously posted on throwing
client side errors in ASP.Net and taking that idea just a little bit farther,
you can throw client side confirmation dialogs. I find this particularly useful
when you want to do something like ask “Are you sure?” when someone clicks a “Delete”
button.
-
Provide a control ( in this case, a button
) on your markup page.<asp:Button ID=”_btnDelete” Runat=”server” Text=”Delete”></asp:Button>
-
In your code behind
page, hook into the control before it renders, say in Page_Load. You want to
dynamically add to the controls attributes and register a little javascript for the
onclick attribute. This example is a simple confirmation dialog.
private> void Page_Load(object sender,
EventArgs e)
{
_btnDelete>>.Attributes.Add( “onclick”, @”return
confirm(‘Are you sure you want to delete this user?’);” );
}
Returning true from this dialog will cause the postback to occur and your handler
for your button will be called. Returning false will not cause a postback.
>>
>>
By the way, if you are wanting to do this to your Delete buttons in a
data grid, you can accomplish it like this:
private void _dgUsers_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs
e)>{
switch( e.Item.ItemType
)>{
case ListItemType.EditItem:>
Button btn
= (Button)e.Item.Cells[3].Controls[0];>
btn
.Attributes.Add( "onclick", @"return
confirm('Are you sure you want to delete this user?');" );>break;>
}
}
>
The “Delete“ button is in the third cell of the datagrid.
I like this technique because it is simple and effective.
>>
>>