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

前端之家收集整理的这篇文章主要介绍了c# – 带有重定向的JavaScript后面的代码警报前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
之后,当我重定向到另一个页面时,如何使我的代码代码中工作?我有一个asp按钮控件,当我点击该按钮时我想要提醒,然后导航到另一个页面.当我的代码中有一个Response.Redirect(在JS代码之前或之后)时,8次尝试都没有.当我评论重定向时,一些(2,7和8)工作.
  1. //Try one
  2. ScriptManager.RegisterStartupScript(this,GetType(),"test","alert('test1');",true);
  3.  
  4. //Try two
  5. ClientScript.RegisterClientScriptBlock(typeof(Page),"test2");
  6.  
  7. //Try three
  8. Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","alertMessage()",true);
  9.  
  10. //Try four
  11. ClientScript.RegisterStartupScript(GetType(),true);
  12.  
  13. //Try five
  14. ClientScript.RegisterStartupScript(GetType(),"javascript: alertMessage(); ",true);
  15.  
  16. //Try six
  17. ClientScript.RegisterClientScriptBlock(GetType(),"<script>alert('test4')</script>");
  18.  
  19. //Try seven
  20. Response.Write("<script>alert('test5');</script>");
  21.  
  22. //Try eight
  23. string script = "alert('test6')";
  24. ScriptManager.RegisterStartupScript(Page,Page.GetType(),"CallMyString",script,true);
  25.  
  26. response.redirect("pageUrlHere");
  27. //With this code above,none of the js functions (alerts) work
  28.  
  29. //response.redirect("pageUrlHere");
  30. //With this commented out,try 2,7 and 8 work.

JS功能

  1. function alertMessage() {
  2. alert('test3');
  3. }

解决方法

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

猜你在找的C#相关文章