【问题标题】:Two Different Javascript Alerts before redirecting in ASP.NET在 ASP.NET 中重定向之前的两个不同的 Javascript 警报
【发布时间】:2016-10-18 22:02:44
【问题描述】:

我想使用 JavaScript 显示不同的警报消息。这是我的代码,但我的警告框不会在重定向之前显示。我尝试了提供的其他示例,但这些示例都只使用一种类型的警报消息。我也使用此 ShowAlertMessage 方法来显示其他类型的警告,其中我不想重定向到任何其他页面。只需给用户一个警告。

If (user creates a new work order)
{
    ShowAlertMessage("Property work order " + txtWorkOrderNumber.Text + " created successfully");
}
else
    ShowAlertMessage("Property work order updated successfully");

Response.Redirect("~/DashBoard.aspx");


public static void ShowAlertMessage(string msg)
{
     Page page = HttpContext.Current.Handler as Page;

     if (page != null)
     {
           string script = "alert(\"'" + msg + "'\");";
           ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", script, true);
     }
}

【问题讨论】:

    标签: javascript c# asp.net redirect alert


    【解决方案1】:

    您发送的是Response.Redirect,这意味着您的回复没有任何内容,而只是要重定向到的 URL。

    为了做你想做的事,你必须将 javascript 写到当前页面,然后一旦警报触发,使用 javascript 移动到一个新的 URL。

    可能类似于以下内容:

    string script = string.Format("alert('{0}'); window.location.href='{1}';",
        msg, ResolveUrl("~/Dashboard.aspx"));
    

    这是 WebForms 问题的一个常见示例 - 很难将客户端和服务器代码正确混合在一起以提供良好的用户体验,这就是为什么我更喜欢完全用 javascript 来完成我的用户体验工作,使用 AJAX做大部分的帖子。

    【讨论】:

    • 问题是我不想在 ShowAlertMessage 中包含我的 Response.Redirect,因为我也将此方法用于其他警告,其中我不需要将用户重定向到任何其他警告页面。
    猜你喜欢
    • 2012-08-25
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    相关资源
    最近更新 更多