积极答复者
ASP.NET,执行Server.Transfer(System.Web.UI.Page)出现页面事件不响应

问题
-
我建立了一个FirstPage.aspx,里面有一个按钮button,其事件为:
Response.Redirect("~/SecondPage.aspx");
现在外部代码生成Page类的FirstPage(其中省略了一些代码):
public class MyHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { if (context != null && context.Server != null) { page1 = BuildManager.CreateInstanceFromVirtualPath("~/FirstPage.aspx", typeof(Page)) as Page; context.Server.Transfer(page1, false); } }
结果发现这种方式生成的FirstPage,按里面的button不能跳转到SecondPage.aspx,只能刷新页面而已。
目前该如何解决这个跳转不成功的问题呢?
- 已移动 ThankfulHeart 2012年6月16日 6:55 ASP.NET技术问题 (发件人:.NET Framework 一般性问题讨论区)
答案
-
不要通过 IHttpHander 进行,直接使用路径就可以了。
context.Server.Transfer("~/FirstPage.aspx");
这样生成的页面如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title> </title></head> <body> <form method="post" action="FirstPage.aspx" id="form1"> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMjA0OTM4MTAwNGRkiG/gyV5H6+lDbsqxhcUFMbsOyT+j0Zq+uKztjmqwc7U=" /> </div> <div class="aspNetHidden"> <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgLZtY30DwKSoqqWD2g2863ko5spNtup03BLcHBnHxnT4GSmOm9emMdbwKUn" /> </div> <div> <input type="submit" name="btn" value="Button" id="btn" /> </div> </form> </body> </html>
冠军
- 已建议为答案 冠 军MVP 2012年6月18日 13:07
- 已标记为答案 moon_prince 2012年6月19日 1:14
全部回复
-
注意一下这种情况下生成的页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title> </title></head> <body> <form method="post" action="MyHandler.ashx" id="form1"> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMjA0OTM4MTAwNGRkiG/gyV5H6+lDbsqxhcUFMbsOyT+j0Zq+uKztjmqwc7U=" /> </div> <div class="aspNetHidden"> <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgLZtY30DwKSoqqWD2g2863ko5spNtup03BLcHBnHxnT4GSmOm9emMdbwKUn" /> </div> <div> <input type="submit" name="btn" value="Button" id="btn" /> </div> </form> </body> </html>
现在表单的提交地址为 MyHandler.ashx,当你点击按钮的时候,不会提交到 FirstPage 页面,也就不会转到 SecondPage 了。
冠军
-
不要通过 IHttpHander 进行,直接使用路径就可以了。
context.Server.Transfer("~/FirstPage.aspx");
这样生成的页面如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title> </title></head> <body> <form method="post" action="FirstPage.aspx" id="form1"> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMjA0OTM4MTAwNGRkiG/gyV5H6+lDbsqxhcUFMbsOyT+j0Zq+uKztjmqwc7U=" /> </div> <div class="aspNetHidden"> <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgLZtY30DwKSoqqWD2g2863ko5spNtup03BLcHBnHxnT4GSmOm9emMdbwKUn" /> </div> <div> <input type="submit" name="btn" value="Button" id="btn" /> </div> </form> </body> </html>
冠军
- 已建议为答案 冠 军MVP 2012年6月18日 13:07
- 已标记为答案 moon_prince 2012年6月19日 1:14
-
不要通过 IHttpHander 进行,直接使用路径就可以了。
context.Server.Transfer("~/FirstPage.aspx");
这样生成的页面如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title> </title></head> <body> <form method="post" action="FirstPage.aspx" id="form1"> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMjA0OTM4MTAwNGRkiG/gyV5H6+lDbsqxhcUFMbsOyT+j0Zq+uKztjmqwc7U=" /> </div> <div class="aspNetHidden"> <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgLZtY30DwKSoqqWD2g2863ko5spNtup03BLcHBnHxnT4GSmOm9emMdbwKUn" /> </div> <div> <input type="submit" name="btn" value="Button" id="btn" /> </div> </form> </body> </html>
冠军
- 已编辑 moon_prince 2012年6月19日 1:15