AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 How To Add JavaScript Delete Confirmation in GridView "Delete" Command Button dynamically?
Posted by Moderator1 on  2/13/2009 3:27:44 AM 11543 Views
In the RowDataBound event of the GridView control, find the Delete Command Button control by specifying the cell index. Then add an attribute to that control has follows.

if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].HasControls())
{
LinkButton lnkbtnDelete = ((LinkButton)e.Row.Cells[1].Controls[0]);
lnkbtnDelete.Attributes.Add("onclick", "return confirm('Do you want to Delete?');");
}
}

Note:
1. The Cell starts with 0 Index.
2. The line "if (e.Row.Cells[1].HasControls())" is to check, if the control is exists or not. This will be useful, when Edit Command Button is used along with Delete Command Button.

Post New Tips/Tricks View all Tips/Tricks

Featured Resources:
Backup and Recovery Best Practices for Microsoft SQL Server 2005
To help you choose from among the available configuration options and backup and recovery procedures, HP has conducted extensive laboratory tests to determine best practices.
The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks - Free 158 Page Preview
Get the most out of this complete question-and-answer book on JavaScript.
100% Free Subscription until Stock lost.View complete list.
Sponsored by