JS Confirm dialog from datagrid template column

Discussion in 'ASP .Net' started by Patrick Delifer, Apr 27, 2004.

  1. 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
    Patrick Delifer, Apr 27, 2004
    #1
    1. Advertising

  2. Patrick Delifer

    Sebastian Guest

    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
    Sebastian, Apr 28, 2004
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:%...
    > 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
    >
    >
    Patrick Delifer, Apr 28, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Baker
    Replies:
    0
    Views:
    6,685
    Tony Baker
    Dec 17, 2003
  2. Dexter
    Replies:
    1
    Views:
    405
    Eliyahu Goldin
    Jan 18, 2005
  3. Ken Varn
    Replies:
    1
    Views:
    963
    Ken Varn
    Aug 19, 2005
  4. Charleees
    Replies:
    2
    Views:
    8,185
    Eliyahu Goldin
    Jun 20, 2006
  5. Ravichandran Mahalingam

    Bound Column or Template Column (w dAdapater?) in DataGrid

    Ravichandran Mahalingam, Jan 9, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    203
    Ravichandran Mahalingam
    Jan 9, 2004
Loading...

Share This Page