日期:2014-05-18  浏览次数:20459 次

问一个很简单的问题。。!!
请教一个很简单的问题:
在后台.aspx.cs页向前台.aspx页面写入一个XML流:
  Response.Clear();
  Response.ContentType = "text/xml ";
  Response.Write ("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); 
  Response.Write("<response>");
  Response.Write("<passed>" + passed +"</passed>");
  Response.Write("<message>" + message + "</message>");
  Response.Write("</response>");
  Response.End(); 
如果我不写最后一句,也就是Response.End(); 我的另一个静态页
JScript code

if(xmlhttp.readyState==4)
            {
                if(xmlhttp.status==200)
                {
                    var mes = xmlhttp.responseXML.getElementsByTagName("message")[0].firstChild.data;
                    var val = xmlhttp.responseXML.getElementsByTagName("passed")[0].firstChild.data;
                    setMessage(mes,val);
                }
                else
                {
                    alert("error:"+xmlhttp.status);
                }
            }


是获取不到message和passed的值的。
写上Response.End(); 就可以,请问为什么?


------解决方案--------------------
你可以把 xmlhttp.responseXML 的内容显示出来看一下,是不是返回 XML 内容不对

如果不加 Response.End() 后面是否还有东西输出了?
------解决方案--------------------
asp.net中.response.write用于在页面中输出内容.而response.end表示终止输出.以及response.end下面的代码不起作用.
原因
Response.End 方法终止页的执行,并将此执行切换到应用程序的事件管线中的 Application_EndRequest 事件。不执行 Response.End 后面的代码行。
此问题出现在 Response.Redirect 和 Server.Transfer 方法中,因为这两种方法均在内部调用 Response.End。 

对于 Response.End,调用 HttpContext.Current.ApplicationInstance.CompleteRequest 方法而不是 Response.End 以跳过 Application_EndRequest 事件的代码执行。 
? 对于 Response.Redirect,请使用重载 Response.Redirect(String url, bool endResponse),该重载对 endResponse 参数传递 false 以取消对 Response.End 的内部调用。例如: 
Response.Redirect ("nextpage.aspx", false);
如果使用此替代方法,将执行 Response.Redirect 后面的代码。 
? 对于 Server.Transfer,请改用 Server.Execute 方法。

一般大家都是这么写的 Response.Write("Server Message !" );
Response.End();如果不加Response.End,用responseText得到的信息就不仅是我们写的了,整个页面的html代码都在里头,处理起来更麻烦。可是我在去掉 Response.End后发现,虽然传回去的数据很多,但第一次响应的速度反而更快。

按照我一直以来的理解,调用Response.End的话,页面在回发时应该少做了不少工作,速度会比不调用快,但现在看来显然不是。我看了下帮助,试了几个办法,发现这样写第一次请求就不会慢了。 Response.Write("Server Message !");
Response.Flush();
Response.Close();
帮助对Response.Close的解释是关闭到客户端的连接。对Response.End的解释是停止该页的执行,并引发Application_EndRequest,而对Application_EndRequest的说明就不怎么详细了,帮助里就找到了这么一句:Application_EndRequest事件使您可以有机会关闭或处置用于该请求的资源。