带有重定向的JavaScript警报后面的代码

时间:2013-12-13 09:16:43

标签: c# javascript asp.net redirect alert

当我重定向到另一个页面之后,如何让我的代码落后于工作?我有一个asp按钮控件,当我点击该按钮时我想要提醒,然后导航到另一个页面。当我的代码中有Response.Redirect时(在JS代码之前或之后),8次尝试都没有。当我评论重定向时,一些(2,7和8)工作。

//Try one
ScriptManager.RegisterStartupScript(this, GetType(), "test", "alert('test1');", true);

//Try two
ClientScript.RegisterClientScriptBlock(typeof(Page), "test", "test2");

//Try three
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "alertMessage()", true);

//Try four
ClientScript.RegisterStartupScript(GetType(), "CallMyFunction", "alertMessage()", true);

//Try five
ClientScript.RegisterStartupScript(GetType(), "CallMyFunction", "javascript: alertMessage(); ", true);

//Try six
ClientScript.RegisterClientScriptBlock(GetType(), "CallMyFunction", "<script>alert('test4')</script>");

//Try seven
Response.Write("<script>alert('test5');</script>");

//Try eight
string script = "alert('test6')";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "CallMyString", script, true);

response.redirect("pageUrlHere");
//With this code above, none of the js functions (alerts) work

//response.redirect("pageUrlHere");
//With this commented out, try 2, 7 and 8 work. 

JS功能:

function alertMessage() {
    alert('test3');
}

2 个答案:

答案 0 :(得分:9)

您可以尝试以下操作:

ScriptManager.RegisterStartupScript(this,this.GetType(),"redirect",
"alert('test 9'); window.location='" + 
Request.ApplicationPath + "/anotherpage.aspx';",true);

答案 1 :(得分:4)

尝试此操作会显示警告并导航  它使用单独的方法再次重用。

    public void ShowAlertAndNavigate(string msg , string destination)
    {
        string alert_redirect_Script = string.Format(@"<script type=""text/javascript"">
                                       alert('{0}');
                                        window.location.href = destination;
                                       </script>", msg);
        ClientScript.RegisterClientScriptBlock(this.GetType(), "alertredirectscript",   alert_redirect_Script, false);
    }