zoukankan      html  css  js  c++  java
  • ASP.NET之Response.Write说

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

    首先我想说最好不要在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 Response property.

    如何验证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。

    如有错误,望大家指正。

  • 相关阅读:
    对《分享一下自己用c++写的小地图》一文的补充
    大神的Blog挂了,从Bing快照里复制过来的备份
    如何创建独立的UE4服务端
    打包如何打包额外文件,比如Sqlite数据库的db文件
    关于如何通过定义自己的CameraManager来控制视角
    Slate中绑定动态数据
    分享一下自己用c++写的小地图
    2016.8看到的一些有用的文章
    如何在插件中添加Actor类
    如何使用的Ue4自带的SQLiteSupport
  • 原文地址:https://www.cnblogs.com/cqcmdwym/p/2805422.html
Copyright © 2011-2022 走看看