ASP.NET之Response.Write说

ASP.NET之Response.Write说

 https://www.cnblogs.com/cqcmdwym/archive/2012/12/06/2805422.html

对于大牛们看到这篇文章千万别喷我哈。

首先我想说最好不要在ASP.NET中用Response.Write()输出一段文字或者类似alert的东西,因为它会把内容放在body之外,html最上面,破坏了整个html。可以用ClientScriptManager的方法来实现这个功能。

其次想说aspnet中Response.Write的实现方式,可能不一定正确,我是这么理解的。先看段代码

复制代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
         fdsfdfdsfd
    </div>

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </form>
</body>
</html>
复制代码
 protected void Button1_Click(object sender, EventArgs e)
 {
        Response.Write("test");
 }

当你点击button时,页面将变成

那如何才能只输出test呢,答案是

Response.Write("test");
Response.End();

问了下做java的人,貌似不是这样,直接write就行。看来ms又帮你封装了层东西。。。应该就是在页面render的时候,填入response东西了

 

 MSDN解释:During the rendering phase, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream of the page's Responseproperty.

如何验证Close的作用呢

复制代码
 protected void Button1_Click(object sender, EventArgs e)
 {
        Response.Write("test");
        Response.End();
 }

 protected override void Render(HtmlTextWriter writer)
 {
        base.Render(writer);
 }
复制代码

在两个事件第一行加断点,根据webform page life cycle,Response.End()后不再调用Render了。所以只输出test,原来的html就不会输出了。

Response.End() 使 Web 服务器停止处理脚本并返回当前结果。文件中剩余的内容将不被处理。对这句话有了新的理解,以前的理解显然不太对。

题外话,对Response的自定义处理的话可以通过Response.Filter来控制,类似于HttpModule。

 

如有错误,望大家指正。

 
 
标签: ASP.NET
好文要顶 关注我 收藏该文  
0
0
 
 
 
« 上一篇:转:《Effective C#》读书笔记——条目17:实现标准的销毁模式<.NET资源管理>
» 下一篇:创智天地半日游
posted @ 2012-12-06 22:48 nickycookie 阅读(724) 评论(0) 编辑 收藏
posted @ 2017-12-12 15:31  sky20080101  阅读(140)  评论(0)    收藏  举报