繁体   English   中英

确认消息后删除 gridview 中的行

[英]Delete row in gridview after confirmation message

以前为了删除我的记录,我将使用双击,但是当涉及到平板电脑或手机时,双击将识别为放大/缩小,因此我更改为带有确认消息的 onclick,问题来了,我该怎么办用户按确定后删除它?

P/S:我没有使用按钮而是通过选择行来删除数据

当前点击代码

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 + "');");

它的完整代码

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 + "');");
    }
}

我仍在通过谷歌搜索答案。 如果之前有人问过同样的问题,请分享链接。 谢谢

只需尝试在aspx代码中为按钮分配OnclientClick ,如下所示

<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>

试试下面的代码

<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>

然后在行数据绑定

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

然后在行删除

 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();
 }

如果下面的行从 gridview 中删除该行

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

并且您想在确认时调用上述功能,然后您可以进行以下更改。

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 + "');}");          
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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