在重定向之前无法打开警报框.ASP.NET

时间:2011-06-28 17:47:33

标签: asp.net

我正在尝试在重定向之前显示警告框,但它不起作用。警报框仅在以下情况下有效 重定向没有完成。

我从Mads Kristensen修改了流行的Alert.Show(“string”)类,如下所示....

public static class Alert
{

    /// <summary> 
    /// Shows a client-side JavaScript alert in the browser. 
    /// </summary> 
    /// <param name="message">The message to appear in the alert.</param> 

    public static void Show(string message)
    {
        // Cleans the message to allow single quotation marks 
        string cleanMessage = message.Replace("'", "\\'");

       //replacing  script string with strSCript
        //string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";

        //added this below
        string strScript = "<script type=\"text/javascript\" language=\"javascript\">";
        strScript += "alert('" + cleanMessage + "');";
        strScript += "window.location.href='http://localhost/Gadgeteer/IncToH/IncToH.zip';";
        strScript += "</script>";


        // Gets the executing web page 
        Page page = HttpContext.Current.CurrentHandler as Page;

        // Checks if the handler is a Page and that the script isn't allready on the Page 
        if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
        {
            page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", strScript);

        }
    }

}
//calling from code behind
Alert.Show("message");

1 个答案:

答案 0 :(得分:1)

您可能在警报功能中出现了一些格式错误,但是失败了,但重定向不是。请查看呈现给浏览器的内容并考虑改进警报消息。如果消息是动态的,请小心避免使用XSS。