简体   繁体   中英

Delete row in gridview after confirmation message

Previously in order to delete my record i will use double click, but when it comes to tablet or phone the double click will identify as zoom in / zoom out, therefore i change to onclick with confirmation message and here comes the problem, how should i delete it after user press ok?

P/S: I didn't use button but by selecting the row to delete the data

current onclick code

e.Row.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this order? ');");

Previous doubleclick delete code

//e.Row.Attributes.Add("ondblclick", "__doPostBack('CView','Select$" + e.Row.RowIndex + "');");

the full code of it

protected void CView_RowDataBound(Object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#cccccc'");
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        e.Row.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this order?');");
        //e.Row.Attributes.Add("ondblclick", "__doPostBack('CView','Select$" + e.Row.RowIndex + "');");
    }
}

I'm still searching the answer through google. If someone else asked the same question before do share me the link. Thank you

Just try to assign the OnclientClick in aspx code for the button as follows

<asp:TemplateField>
      <ItemTemplate>
            <asp:Button ID="deletebtn" runat="server" CommandName="Delete" 
             Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this order? ');" />
      </ItemTemplate>
</asp:TemplateField>

Try the following code

<asp:GridView ID="GridView1" runat="server" OnRowDeleting="OnRowDeleting" AutoGenerateColumns = "false" OnRowDataBound = "OnRowDataBound">
<Columns>
    <asp:BoundField DataField="..columnname.." HeaderText="...." />
    <asp:BoundField DataField="..columname.." HeaderText="...." />
    <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
</Columns>

Then on Row DataBound

   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; };";
        }
    }
    }
    }

Then on row deleting

 protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
 {
     // delete code
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["dt"] as DataTable;
dt.Rows[index].Delete();
ViewState["dt"] = dt;
BindGrid();
 }

If below line deletes the row from gridview

e.Row.Attributes.Add("ondblclick", "__doPostBack('CView','Select$" + e.Row.RowIndex + "');");

and you want to call above functionality on confirmation then you can do following changes.

if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#cccccc'");
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        e.Row.Attributes.Add("onclick", "var vconfirm = confirm('Are you sure you want to delete this order?'); if (vconfirm){ __doPostBack('CView','Select$" + e.Row.RowIndex + "');}");          
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM