473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

JS Confirm dialog from datagrid template column

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
Nov 18 '05 #1
2 2086
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
Nov 18 '05 #2
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" <po*****@mail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeremy | last post by:
I have a datagrid that users interact with to add/edit/delete, but before the .aspx submits for the delete action I want to pop up a JavaScript confirm box. If the user clicks OK on that box I...
4
by: ShadowsOfTheBeast | last post by:
hi oliver thanks for your help i kinda figured that out minutes afterwards...but what i am actually trying to do is this: i have a listbox control that gets its data from another listbox (hence...
4
by: Richard Roche | last post by:
Is it possible to use a drop combo instead of a text box when using the EditCommand in the Datagrid? Many table columns are bound to 'lookup' tables, user's don't care about the foreign keys,...
2
by: damonf | last post by:
I'm currently trying to add an ASP hyperlink to a template column in a datagrid. The normal hyperlink column doesn't give me the ability to add attributes to the item. In my grid there are four...
4
by: Stephan Bour | last post by:
Hi, I have a datagrid databound to a SQL query. I'd like to allow editing of some columns but not all. Is there a way to turn off the conversion of the datagrid cells to textboxes for some columns...
1
by: RN | last post by:
Hi. I used this article to add a template column to a datagrid that is entirely created with code: ...
1
by: Ken Varn | last post by:
I have a problem where my DataGrid would not maintain the ViewState of my databound rows. I finally narrowed down the problem. If my first column is a template column, the view state for the...
8
by: Matt | last post by:
Guys I could really use some help with this. I think that it's probably a simple solution but I haven't been able to find anything. I have a datagrid on my form that I populate via code like...
4
by: mamun | last post by:
Hi All, I have the following situation and am looking for answer in C#. I have a datagrid and putting checkbox next to each record. In the header I have a Delete button. I want users to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.