JS Confirm dialog from datagrid template column

P

Patrick Delifer

Hi,

I am trying to implement a JS confirm dialog when a user is deleting
something off my Datagrid.
The problem is that I don't have a Delete button in which case I could write
the following function:

btnDelete.Attributes.Add("onClick", "if(confirm('Are you sure you want to
delete this order?')){}else{return false}");

I have a Delete Command which is evaluated in my ItemCommand like so:

private void dgOrder_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{ if ((e.Item.ItemType == ListItemType.AlternatingItem) ||{...
if (e.CommandName == "Delete")
{
//delete the order//
}
}

Now in my html page, the Delete column is a Template Column and not a button
Column. How can I implement an onClick event for my template column, when
there is no button defined in my aspx.cs page.
My template column is:

<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton id="Imagebutton1" runat="server" NAME="Imagebutton1"
CausesValidation="false" CommandName="Delete"
alt="Delete" ImageUrl="Images/deleteord.GIF"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>

Thanks a lot
Pat
 
S

Sebastian

It's very easy.

Since you have Imagebutton1 in TemplateColumn, you have to add JS to onclick
(just like you wrote).
You can do this during DataGrids ItemDataBound event, like this:
//Find delete button
ImageButton Imagebutton1 = (ImageButton )e.Item.FindControl("Imagebutton1");

//We don't want to serach for this button in header or footer
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType ==
ListItemType.AlternatingItem))

{

//Add on click event
Imagebutton1 .Attributes["onClick"] =
"javascript:your_JS_delete_command();";

}

You might add id attibute to IMageButton too.

Sebastian
 
P

Patrick Delifer

Thanks Sebastian. Works well.
I also tried this approach without looking for the control:

e.Item.Cells[17].Attributes.Add("onClick","if(confirm('Are you sure you want
to delete this order?')){}else{return false}");



works the same.

Thx.

Pat



I was trying e.item
Sebastian said:
It's very easy.

Since you have Imagebutton1 in TemplateColumn, you have to add JS to onclick
(just like you wrote).
You can do this during DataGrids ItemDataBound event, like this:
//Find delete button
ImageButton Imagebutton1 =
(ImageButton )e.Item.FindControl("Imagebutton1");
//We don't want to serach for this button in header or footer
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType ==
ListItemType.AlternatingItem))

{

//Add on click event
Imagebutton1 .Attributes["onClick"] =
"javascript:your_JS_delete_command();";

}

You might add id attibute to IMageButton too.

Sebastian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top