Community discussion forum

Confirmation box when deleting

  • 1 year ago

    when the delete button is pressed in my gridview i need a confirmation box to ask the user if they want to delete the row.

    How?

  • 1 year ago

    just write in OnClientClick="return confirm('Are you sure to delete?';)" on your delete button.

  • 1 year ago

    that dosnt work mate all i get is 'OnClientClick dosnt exist in the current context'

     +

    i cant use that in the gridview

    i am using ASP.Net and C# if that helps

  • 1 year ago

    I think what sainyam was trying to say is that you need a little piece of javascript added to the delete button ("return confirm('Are you sure to delete?';)")

    Usually, this is added to the pages html section with a user event like "onClick". However, you can add it through the RegisterClientScript method in code if you prefer.

    HTH 

    Joe

  • 1 year ago

    could you provide an example if you dont mind as this is my first time using ASP.Net and i never use javascript.

    cheers

  • 1 year ago

    Which version of .Net are you using?

    Joe 

  • 1 year ago

    Visual Studio 2005

    im writing in C# and ASP.Net

  • 1 year ago

     hi thedrunkendevilx,

    can u plz post the desing code of your datagrid..... 

  • 1 year ago

    Then the earlier post was correct (with a slight spelling mistake):

                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="ButtonDelete" runat="server" Text="Delete" OnClientClick="BLOCKED SCRIPTreturn confirm('Are you sure to delete?');" />
                        </ItemTemplate>
                    </asp:TemplateField>

    I've basically included a button in a template column in the grid and assigned the earlier javascript to it... works fine.

    HTH

    Joe 

    "BLOCKED SCRIPT" = javascript with colon

  • 1 year ago

     Thanks joe90......that's what i was trying to explain

  • 1 year ago

    that is what i am trying to do its just that i have a command field not a template field

  • 1 year ago

    I figured as much sainyam... javascript can be a real pain with case and 'exactness' - very unforgiving IMHO.

    At least 2008 has a javascript debugger in it - which should help many people out.

    Joe 

  • 1 year ago

     ya thats right...

  • 1 year ago

    so how do i do it with a command button

  • 1 year ago

    Take a look here:

    http://flux88.com/PermaLink,guid,0d217ee6-02f0-41c8-bc54-af30ec448714.aspx

    Though you may find this easier to follow:

            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    IButtonControl btn = e.Row.Cells[5].Controls[0] as IButtonControl;
                    ((WebControl)btn).Attributes.Add("onClick", "return confirm('Are you sure you want to delete?');");
                }
            }

    (you'll need to change the "5" to whatever column your button is in) 

    Joe 

  • 1 year ago

    i now have the message box poping up but it does nothing when i click ok. C# here is the code if it helps.

    protected void dgvTimesheets_RowDataBound(object sender, GridViewRowEventArgs e)

    {

    if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer)

    {

    if ((e.Row.Cells[7].Controls[0] as CheckBox).Checked)

    {

    e.Row.CellsMusic.Visible =
    false;e.Row.Cells[9].Visible = true;

    }

    else

    {

    e.Row.CellsMusic.Visible =
    true;e.Row.Cells[9].Visible = false;

    }

    }

    if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer)

    {

    (e.Row.Cells[0].Controls[0]
    as ImageButton).Attributes.Add("onclick", "return ConfirmDelete();");

    }

    }

  • 1 year ago

    Have you coded anything into the Command event?

    Joe 

  • 1 year ago

    i done it now all i need to know is how to stop the message from coming up twice because there is a bug with .net that fires the rowdeleting event 2 times.

     thanks for all your help

     

    SamSmiley Face

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback