确认消息后删除gridview中的行

时间:2015-10-23 04:05:20

标签: c# asp.net gridview

以前为了删除我的记录,我将使用双击,但是当涉及到平板电脑或手机时,双击将识别为放大/缩小,因此我更改为onclick并显示确认消息,此处出现问题,用户按下确定后如何删除它?

  

P / S:我没有使用按钮,而是选择要删除数据的行

当前的onclick代码

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

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

3 个答案:

答案 0 :(得分:0)

只需尝试为按钮的aspx代码分配<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> ,如下所示

MPVolumeView

答案 1 :(得分:0)

尝试以下代码

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

然后在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; };";
        }
    }
    }
    }

然后删除行

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

答案 2 :(得分:0)

如果以下行删除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 + "');}");          
    }