how to add delete confirmation prompt for command field in detail view?

All we need is an easy explanation of the problem, so here it is.

I want to prompt the user for confirmation when he tries to delete a record in a detail view? I have command filed in which showDeletebutton set to true.

I found how to do the confirmation for gridview, but how can I modify to match detail view?

Code:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
    // loop all data rows
    foreach (DataControlFieldCell cell in e.Row.Cells)
    {
       // check all cells in one row
       foreach (Control control in cell.Controls)
       {
            // Must use LinkButton here instead of ImageButton
            // if you are having Links (not images) as the command button.
            ImageButton button = control as ImageButton;
            if (button != null && button.CommandName == "Delete")
                // Add delete confirmation
                button.OnClientClick = "if (!confirm('Are you sure " + 
                       "you want to delete this record?')) return;";
        }
    }
}
}

Anybody?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

     <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
        .....
            <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
            <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
                SortExpression="Quantity" />
            <asp:TemplateField ShowHeader="False">
                 <ItemTemplate>
                    <asp:LinkButton  ID="LinkButton2" runat="server" CausesValidation="False" 
                        CommandName="New" Text="New"></asp:LinkButton>

                    <asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="False" 
                        CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record');"></asp:LinkButton>

                </ItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView

This can be done easily on the markup code. I simply added the js code to the onClientClick property of the delete button:

OnClientClick="return confirm('Are you sure you want to delete this record');"

Or if you want do this in the code behind:

 protected void DetailsView1_DataBound(object sender, EventArgs e)
  {
    LinkButton bttn = (LinkButton)DetailsView1.FindControl("lnkDelete");
    bttn.OnClientClick = "return confirm('Are you sure you want to delete this record!');";
  }

Method 2

I found the answer to my question.

My answer:

 protected void DViewComputer_DataBound1(object sender, EventArgs e)
{
    int noRow = DViewComputer.Rows.Count - 1;//get the no of record

    if (noRow >0)
    {
        Button button = (Button)(DViewComputer.Rows[noRow].Cells[0].Controls[2]);

        // Add delete confirmation
        ((System.Web.UI.WebControls.Button)(button)).OnClientClick = "if (!confirm('Are you sure " +
                               "you want to delete this record?')) return;";

    }
}

Anyways thanks for your help guys.

Method 3

   foreach (Control control in cell.Controls)
   {
        // Must use LinkButton here instead of ImageButton
        // if you are having Links (not images) as the command button.
        ImageButton button = control as ImageButton;
        if (button != null && button.CommandName == "Delete")
            // Add delete confirmation
            button.Attributes.Add("onclick","your javascript here");
    }

Method 4

Please see the below URL……
http://www.codeproject.com/Articles/32756/ASP-NET-GridView-delete-confirmation-using-asp-Com

Method 5

This corrects the OP’s solution. The code was translated from the code found here: http://forums.aspfree.com/net-development-11/confirm-button-when-deleting-detailsview-120113-2.html

protected void dvEvent_DataBound(object sender, EventArgs e)
{

    int commandRowIndex = dvEvent.Rows.Count - 1;
    if (commandRowIndex > 0)
    {
        DetailsViewRow commandRow = dvEvent.Rows[commandRowIndex];
        DataControlFieldCell cell = (DataControlFieldCell)commandRow.Controls[0];

        foreach (Control ctrl in cell.Controls)
        {
            if (ctrl is ImageButton)
            {
                ImageButton ibt = (ImageButton)ctrl;
                if (ibt.CommandName == "Delete")
                {
                    ibt.ToolTip = "Click here to Delete";
                    ibt.CommandName = "Delete";
                    ibt.Attributes["onClick"] = "if (!confirm('Are you sure " +
                                "you want to delete this record?')) return;";
                }
            }
        }
    }
}

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply