locked
How to add code for 'Are you sure to delete?' into the data-bound controls? RRS feed

  • Question

  • User-830563764 posted

    For the data-bound controls, e.g. GridView, FormView, DetailsView, etc., the
    AutoGenerateDeleteButton can be set to true and Delete linkbutton be coded.
    How to add the codes for something like 'Are you sure you want to delete?"
    before the data is deleted?

    I am working on VS2010 ASP.net 4.

    Friday, May 27, 2016 3:48 PM

Answers

  • User36583972 posted

    Hi wonjartran,

    You can add LinkButton Attributes["onclick"] on GridView1_RowDataBound method. The following code for your reference.

    HTML:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" />
                        <asp:BoundField DataField="Product" HeaderText="Product" />
                        <asp:BoundField DataField="Version" HeaderText="Version" />
                        <asp:BoundField DataField="Description" HeaderText="Description" />        
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="CSDelete" runat="server" CommandName="CustomDelete" Text="Delete"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>

    ASPX.CS:

    protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack )
                {
                    bind();
                }
    
            }
    
            private void bind()
            {
                DataTable tblDatas = new DataTable("Datas");
                DataColumn dc = null;
                dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
                dc.AutoIncrement = true;//
                dc.AutoIncrementSeed = 1;//
                dc.AutoIncrementStep = 1;//
                dc.AllowDBNull = false;
                dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
                dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
                dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
                DataRow newRow;
                for (int i = 0; i < 5; i++)
                {
                    newRow = tblDatas.NewRow();
                    newRow["Product"] = "Count" + i.ToString();
                    newRow["Version"] = "2.0";
                    newRow["Description"] = "Hello boy";
                    tblDatas.Rows.Add(newRow);
                }
                ViewState["dt"] = tblDatas;
                GridView1.DataSource = tblDatas;
                GridView1.DataBind();
    
    
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string item = e.Row.Cells[0].Text;
                    foreach (LinkButton button in e.Row.Cells[4].Controls.OfType<LinkButton>())
                    {
                        if (button.CommandName == "CustomDelete")
                        {
                            button.Attributes["onclick"] = "if(!confirm('Are you sure you want to delete: " + item + "?')){ return false; };";
                        }
                    }
    
                }
            }
    
            protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName.Equals("CustomDelete"))
                {
                    GridViewRow oItem = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                    int RowIndex = oItem.RowIndex;
    
                    Response.Write(oItem.Cells[0].Text+""+ oItem.Cells[1].Text);
    
                }
    
            }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 28, 2016 6:14 AM

All replies

  • User-830563764 posted

    Because I usually code the data-bound control using the Design menu on the VS2010,
    the Edit/Delete/Insert buttons are added with the Linkbuttons.  Is there a way to add
    the confirmation codes into the <asp:LinkButton>?

    Friday, May 27, 2016 4:39 PM
  • User-830563764 posted

    Got it.

    Added OnClientClick="return confirm('Are you sure you want to delete')," CommandArgument=" "

    to <asp:LinkButton>

    Friday, May 27, 2016 7:07 PM
  • User36583972 posted

    Hi wonjartran,

    You can add LinkButton Attributes["onclick"] on GridView1_RowDataBound method. The following code for your reference.

    HTML:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" />
                        <asp:BoundField DataField="Product" HeaderText="Product" />
                        <asp:BoundField DataField="Version" HeaderText="Version" />
                        <asp:BoundField DataField="Description" HeaderText="Description" />        
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="CSDelete" runat="server" CommandName="CustomDelete" Text="Delete"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>

    ASPX.CS:

    protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack )
                {
                    bind();
                }
    
            }
    
            private void bind()
            {
                DataTable tblDatas = new DataTable("Datas");
                DataColumn dc = null;
                dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
                dc.AutoIncrement = true;//
                dc.AutoIncrementSeed = 1;//
                dc.AutoIncrementStep = 1;//
                dc.AllowDBNull = false;
                dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
                dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
                dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
                DataRow newRow;
                for (int i = 0; i < 5; i++)
                {
                    newRow = tblDatas.NewRow();
                    newRow["Product"] = "Count" + i.ToString();
                    newRow["Version"] = "2.0";
                    newRow["Description"] = "Hello boy";
                    tblDatas.Rows.Add(newRow);
                }
                ViewState["dt"] = tblDatas;
                GridView1.DataSource = tblDatas;
                GridView1.DataBind();
    
    
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string item = e.Row.Cells[0].Text;
                    foreach (LinkButton button in e.Row.Cells[4].Controls.OfType<LinkButton>())
                    {
                        if (button.CommandName == "CustomDelete")
                        {
                            button.Attributes["onclick"] = "if(!confirm('Are you sure you want to delete: " + item + "?')){ return false; };";
                        }
                    }
    
                }
            }
    
            protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName.Equals("CustomDelete"))
                {
                    GridViewRow oItem = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                    int RowIndex = oItem.RowIndex;
    
                    Response.Write(oItem.Cells[0].Text+""+ oItem.Cells[1].Text);
    
                }
    
            }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 28, 2016 6:14 AM
  • User1631312260 posted

    You can use ConfimationExtender of AJAX as well as use JQUery

    here, we have simple example of ConfimationExtender

    https://dotprogramming.blogspot.com/2015/09/show-confirmation-message-in-aspnet-c.html

    Tuesday, May 31, 2016 1:00 PM