为什么不't警报执行

Why doesn't the alert execute?

本文关键字:执行 为什么不      更新时间:2023-09-26

我可以控制是否已经存在评论,如果存在,我想警告访客,如果此人在警报中单击"确定"/"是",评论将被覆盖,如果此人按"否"/"取消",评论不会更新。但它不起作用,在调试过程中,警报行只是经过并更新,没有任何警报。

if (ReviewExist(StoreID, UserID) != 0)
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "confirm('Are you sure?');", true);
    UpdateStoreReview(Description);
    Response.Redirect("Default");
}
else      
{
    AddStoreReview(Description);
}

这是因为Response.Redirect将控件重定向到另一个页面,而该页面将忽略RegisterScript

你需要做的是,如果你试图在按钮点击(更新按钮)中实现这一点,那么在Page_Load中尝试

if(!IsPostBack)    
{
 btnUpdate.Attributes.Add("OnClick","confirm('Are you sure?');");
}

然后可以将上述代码更改为

if (ReviewExist(StoreID, UserID) != 0)
{
    UpdateStoreReview(Description);
    Response.Redirect("Default");
}
else      
{
    AddStoreReview(Description);
}

注意我认为您需要重定向到Default.aspx;但是您在response.redirect中缺少.aspx。

您需要脚本中的脚本标记。

ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "<script type='text/javascript'>confirm('Are you sure?');</script>", true);

如果您使用ASP.net

<asp:Button ID="_btnSalvar" runat="server" Confirm="False" ConfirmType="None" Text="Are you sure?" Width="131px" OnClick="_btnSalvar_Click" />