我使用Response.Write将一些html脚本写入我的页面后,我必须重新加载我的页面。使用Response.Write后我使用Response.Flush(),然后使用Response.End()。代码在一台服务器上正常运行,当我将其部署到另一台服务器时,它无法正常工作。 据我所知,代码执行必须在执行Response.End后停止,但它现在以所需的方式运行。
Response.ClearContent();
Response.Write("Write some html, script content here");
Response.Flush();
Response.End();
使用Respose.Redirect(Request.RawUrl)将不起作用,并将抛出一个异常,说明一旦标题发布后重定向就不会发生......
有人可以告诉我,为什么代码执行没有被Response.End()??
停止编辑:实际代码
protected void Page_Load(object sender, EventArgs e)
{
if (Session["localTime"] == null && String.IsNullOrEmpty(Request.Params["localTime"]))
{
// then clear the content and write some html, a javascript code which submits the local time
Response.ClearContent();
Response.Write(@"<form id='local' method='post' name='local'>
<input type='hidden' id='localTime' name='localTime' />
<script type='text/javascript'>
document.getElementById('localTime').value = new Date();
document.getElementById('local').submit();
</script>
</form>");
//
Response.Flush();
// end the response so PageLoad, PagePreRender etc won't be executed
Response.End();//Page must reload after this line.
}
else
{
// if the request contains the localtime, then save it in Session
if (Request.Params["localTime"] != null)
{
Session["localTime"] = Request.Params["localTime"];
Response.Write(Request.Params["localTime"] + " ~2");
// and redirect back to the original url
Response.Redirect(Request.RawUrl);
}
}
}