• .NET服务端持续输出信息到客户端


    由于有时查询内容时间长,可以将查询内容逐一显示出来,以达到用户可以提前查看部分内容的形式来提高用户的体验,那么asp.net如何实现连续不断向客户端显示内容?

    首先在此,我们不讨论客户端Ajax拉拽的方式解决此问题!提供如下两种方式:

    方式一(推荐使用):

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    #region 命名空间
    
    using System.Threading;
    using System.Text;
    
    #endregion
    
    namespace ContinuousExport
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //由于asp.net持续输出到客户端
                //注意:(IE内核浏览器)下需要字符达到256个字符以上(可以提前输出样式,控制后面信息样式)
                //才能想客户端即时发送新的信息
                StringBuilder sbResponse = new StringBuilder();
                sbResponse.Append("<style type=\"text/css\">span{color:Red;}</style>");
                while (sbResponse.Length < 257)
                {
                    sbResponse.Append(" ");
                }
                Response.Write(sbResponse.ToString());
                Response.Flush();
                int j = 0;
                while (true)
                {
                    j++;
                    Response.Write("<span>" + j + "</span>\t");
                    Response.Flush();
                    //1秒输送一次(模拟复杂计算耗时)
                    Thread.Sleep(1000);
                }
            }
        }
    }
    复制代码

    显示效果截图如下:

    浏览器:搜高高速浏览器(兼容模式)--兼容模式为IE内核浏览器

    效果:每隔一秒钟显示一个数字的效果--此时页面仍然在加载状态

    方式二(不推荐,原因:使用重载Render方法,不能在继续对服务器控件指定相关信息,如赋值等...):

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    #region 命名空间
    
    using System.Threading;
    using System.Text;
    
    #endregion
    
    namespace ContinuousExport
    {
        public partial class ExportMain : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            protected override void Render(HtmlTextWriter writer)
            {
                base.Render(writer);
                //输出后面信息的样式
                Response.Write("<style type=\"text/css\">span{color:Red;}</style>");
                Response.Flush();
                int j = 0;
                while (true)
                {
                    j++;
                    Response.Write("<span>" + j + "</span>\t");
                    Response.Flush();
                    //1秒输送一次(模拟复杂计算耗时)
                    Thread.Sleep(1000);
                }
            }
        }
    }
    复制代码

    显示效果如上图。

    附上源码:ContinuousExport.zip

    作者:曾庆雷
    出处:http://www.cnblogs.com/zengqinglei
    本页版权归作者和博客园所有,欢迎转载,但未经作者同意必须保留此段声明, 且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利

      

     
     
  • 相关阅读:
    Dubbo源码分析之ExtensionLoader加载过程解析
    大型网站系统与java中间件实践-阅读笔记
    多线程编程-设计模式之保护性暂挂(Guarded Suspesion)模式
    多线程编程-设计模式之不可变对象模式
    Spring技术内幕阅读笔记(一)
    mysql存储过程语法及实例
    Spring-boot官方案例分析之data-jpa
    Spring-boot官方案例分析之log4j
    Spring Boot应用的测试——Mockito
    linux系统安装redis
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2749126.html
Copyright © 2020-2023  润新知