在确认消息后删除gridview中的行
本文关键字:gridview 删除 确认 消息 | 更新日期: 2023-09-27 18:18:51
以前为了删除我的记录,我会使用双击,但是当涉及到平板电脑或手机时,双击将识别为放大/缩小,因此我更改为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 + "');");
}
}
我还在用谷歌搜索答案。如果有人问了同样的问题,请把链接分享给我。谢谢你
如果下面一行从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 + "');}");
}
尝试为按钮分配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>
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; };";
}
}
}
}
然后删除
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();
}