0

I'm using ASP C# VS 2015. I managed to generate a gridview with data from MySQL. The update and delete command appear as links and I managed to hooked them up to the events properly.

Now I would like to add a feature to add confirmation before delete plus a message (textbox) for reason of deletion?

How do I do that in .NET?

I'm well versed in PHP and jQuery but still in learning curve in .NET. Thanks in advance.

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="id"
            OnRowDataBound="OnRowDataBound" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
            OnRowUpdating="OnRowUpdating" EmptyDataText="No records has been added." OnRowDeleting="OnRowDeleting" OnRowDeleted="OnRowDeleted">
   <Columns>
       <asp:TemplateField HeaderText="Id" ItemStyle-Width="20">
           <ItemTemplate>
               <asp:Label ID="lblId" runat="server" Text='<%# Eval("id") %>'></asp:Label>
           </ItemTemplate>
           <ItemStyle Width="20px"></ItemStyle>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="Email">
           <ItemTemplate>
               <asp:Label ID="lblEmail" runat="server" Text='<%# Eval("email") %>'></asp:Label>
           </ItemTemplate>
       </asp:TemplateField>
       <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" DeleteText="Reject" headertext="Controls"></asp:CommandField>
   </Columns>
</asp:GridView>
RonPringadi
  • 1,294
  • 1
  • 19
  • 44
  • Possible duplicate of [How to add a "confirm delete" option in ASP.Net Gridview?](https://stackoverflow.com/questions/4793955/how-to-add-a-confirm-delete-option-in-asp-net-gridview) –  Nov 16 '17 at 15:38

1 Answers1

1

Try this

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string item = e.Row.Cells[0].Text;
        foreach (Button button in e.Row.Cells[2].Controls.OfType<Button>())
        {
            if (button.CommandName == "Delete")
            {
                button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + item + "?')){ return false; };";
            }
        }
    }
}
R. Bandi
  • 113
  • 11