Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 确认消息后删除gridview中的行_C#_Asp.net_Gridview - Fatal编程技术网

C# 确认消息后删除gridview中的行

C# 确认消息后删除gridview中的行,c#,asp.net,gridview,C#,Asp.net,Gridview,以前,为了删除我的记录,我将使用双击,但当涉及平板电脑或手机时,双击将识别为放大/缩小,因此我更改为带有确认消息的onclick,出现了一个问题,在用户按下ok后,我应该如何删除它 p/S:我没有使用按钮,而是通过选择行来删除数据 当前onclick代码 e.Row.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this order? ');"); 上一个双击删除代码 //e.Row.Att

以前,为了删除我的记录,我将使用双击,但当涉及平板电脑或手机时,双击将识别为放大/缩小,因此我更改为带有确认消息的onclick,出现了一个问题,在用户按下ok后,我应该如何删除它

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

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

请尝试在
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>

如果在第行下方,则从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 + "');}");          
    }

不,您应该在设计端为delete添加Onclick事件@蝶形按钮:通过将响应分配给变量来处理
确认
响应。检查我的答案。感谢你的回答。但仅供参考,我没有使用按钮删除,我希望在删除行之前单击所选数据并收到删除确认消息。:)@你在一行上点击的蝴蝶?或者触发一个按钮来删除?很抱歉我没有把问题说清楚。我想做的是单击所选行以删除该行中的数据。@TheButterfly请参考此项您可能需要
selectedchanged
来触发Onclick@TheButterfly确保接受正确的答案。很遗憾,我没有用按钮来触发确认。但我还是会保留它以备将来参考!谢谢,我试过了,但还是没有删除记录。我将尝试使用函数。无论如何,谢谢你的帮助:等等,你只是打字错误!应该只有1},但您将其改为2。这对我有用!非常感谢你<代码>“');}}”)应该是
“');}”)可能是
\u doPostBack
事件未启动。明白了,有个小错误,我加了一个括号。
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 + "');}");          
    }