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

前端之家收集整理的这篇文章主要介绍了c# – 带有重定向的JavaScript后面的代码警报前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
之后,当我重定向到另一个页面时,如何使我的代码代码中工作?我有一个asp按钮控件,当我点击该按钮时我想要提醒,然后导航到另一个页面.当我的代码中有一个Response.Redirect(在JS代码之前或之后)时,8次尝试都没有.当我评论重定向时,一些(2,7和8)工作.
//Try one
ScriptManager.RegisterStartupScript(this,GetType(),"test","alert('test1');",true);

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

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

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

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

//Try six
ClientScript.RegisterClientScriptBlock(GetType(),"<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');
}

解决方法

您可以尝试以下方法
ScriptManager.RegisterStartupScript(this,this.GetType(),"redirect","alert('test 9'); window.location='" + 
Request.ApplicationPath + "/anotherpage.aspx';",true);

猜你在找的C#相关文章