Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Microsoft: ASP.NET FAQ

Best Practices

Adding a confirmation script to a datagrid button by Alcar
Posted: 10 Jun 02 (Edited 4 Dec 02)

Greetings all,

many times I have seen this question posted in different forums. I, myself had asked it time ago and eventually found a solution.

Is there a way to add a confirmation message to a delete button of my datagrid?

There sure is. The javascript is pretty easy:
javascript: return confirm('Are you sure you want to delete this record?');

The problem is adding it to a button that is created on-the-fly. The best way to access this button is by casting it after it's created and before it's pressed, and the only place where we can do this is in the ItemCreated Event:

private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  ListItemType lit = e.Item.ItemType;
  if (lit == ListItemType.Item)
    Button btnDelete = (Button) e.Item.FindControl("btnDelete");
    btnDelete.Attributes.Add("OnClick", "javascript: return confirm('Are you sure you want to delete this record?');");

et voila'
now we have a delete button that will ask for a confirmation before executing it's code.

I hope this will help most of you, and I hope this Faq finds you in great health.

Happy Coding!

Daren J. Lahey

Back to Microsoft: ASP.NET FAQ Index
Back to Microsoft: ASP.NET Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close