C# 如何在单击确认消息true时删除行?

C# 如何在单击确认消息true时删除行?,c#,javascript,jquery,asp.net,C#,Javascript,Jquery,Asp.net,我有一个asp gridview。在数据绑定行上,我添加onclick事件以查看项目详细信息。我也有删除按钮。问题是当我单击“项目删除”时,它会删除项目,但也会重定向到项目预览页面。它不应执行回发和重定向 这是我的密码 protected void Grid_DataBound(object sender, GridViewRowEventArgs e) { e.Row.Attributes["onClick"] = String.Format

我有一个asp gridview。在数据绑定行上,我添加onclick事件以查看项目详细信息。我也有删除按钮。问题是当我单击“项目删除”时,它会删除项目,但也会重定向到项目预览页面。它不应执行回发和重定向

这是我的密码

 protected void Grid_DataBound(object sender, GridViewRowEventArgs e)
        {

                e.Row.Attributes["onClick"] = String.Format("location.href='" + "/Url/page.aspx?id=" + "{0}", DataBinder.Eval(e.Row.DataItem, "Id") + "'");
            }
删除按钮

public void GvDeleteHabit(object source, CommandEventArgs e)
{
    int id= Convert.ToInt32(e.CommandName);
    Delete(id);
}


<asp:LinkButton OnCommand="Delete" title="Delete" OnClientClick="javascript:return confirm('Do you want to delete?')"
CommandName='<%# Eval("Id")%>' runat="server" ID="BtnDelete"></asp:LinkButton>
public void gvdeleteHabity(对象源,CommandEventArgs e)
{
int id=Convert.ToInt32(如CommandName);
删除(id);
}
在客户端,我有确认消息。所以我试图实现的是,当用户单击“删除”按钮时,是的,我需要从行中删除onclick事件并删除项


如果用户单击“否”,则停止加载页面。因此,用户可以单击行上的某个位置并查看项目的详细信息

尝试从
OnClientClick
中删除
javascript:
。只有在
href
属性中使用javascript时才需要它。

您遇到的问题是,即使在您单击“删除”时,仍会在行上注册为单击事件,从而关闭重定向。您应该将重定向放入函数中,并传入click事件的源。如果源是您的按钮,请退出该功能

onRowClick = function(source){
   if (source.element.type == "button") // this may not be correct, but you get the point
       return confirm('Are you sure you want to delete this item');
   //do your redirect here
}

e.Row.Attributes["onclick"] = "onRowClick(this);";
在HTML标记中:

<asp:LinkButton OnCommand="Delete" title="Delete" OnClientClick="javascript:return onRowClick(this);"
CommandName='<%# Eval("Id")%>' runat="server" ID="BtnDelete"></asp:LinkButton>

因为第行也有onclick处理程序,所以您必须基本上在单击“删除”按钮时停止事件传播,尝试此操作,它将解决问题

ASPX更改

<asp:LinkButton OnCommand="Delete" title="Delete" OnClientClick="javascript:return confrimDelete(e);"
CommandName='<%# Eval("Id")%>' runat="server" ID="BtnDelete"></asp:LinkButton>
function confirmDelete(){
   var event = e || window.event;
   if (event.stopPropagation) {
     event.stopPropagation();
   } else {
     event.cancelBubble = true;
   } 

   return confirm('Do you want to delete?');
}

查找并更改执行重定向的代码?我看不出有任何东西可以这样做。