Groups | Blog | Home
all groups > asp.net > january 2005 >

asp.net : Add a confirm box to a LinkButton in datagrid


Danny Ni
1/28/2005 10:27:33 PM
Hi,

I have a LinkButton inside a datagrid, when this LinkButton is clicked, the
program deleete a record in database. I would like to add a confirmation on
client side. I know I can do Attributes.Add("onclick",...) to Buttons not in
datagrid. But how do I do it for LinkButton in datagrid?

TIA

Mark Rae
1/29/2005 7:34:21 AM
[quoted text, click to view]

public void dgrd_ItemCreated(Object sender, DataGridItemEventArgs e)
{
LinkButton cmdDelete;
if((e.Item.ItemType == ListItemType.Item)
|| (e.Item.ItemType == ListItemType.AlternatingItem)
|| (e.Item.ItemType == ListItemType.EditItem))
{
cmdDelete = (LinkButton)e.Item.Cells[0].Controls[0];
cmdDelete.Attributes.Add("onclick", "return confirm('Are you sure you
want to delete this record?');");
}
}

The above example assumes that your LinkButton is the first Control in the
first column of the datagrid - change the e.Item.Cells[0].Controls[0] values
accordingly if it is not.

AddThis Social Bookmark Button