The. NET Server continuously outputs information to the client.

Source: Internet
Author: User

Because sometimes the query content takes a long time, the query content can be displayed one by one to improve the user experience by allowing users to view part of the content in advance, so how does Asp.net achieve continuous display of content to the client?

First, we will not discuss how to solve this problem by pulling the client Ajax! The following two methods are provided:

Method 1 (recommended ):

 Using  System;  Using  System. Collections. Generic;  Using  System. Web;  Using  System. Web. UI;  Using  System. Web. UI. webcontrols;  # Region Namespace 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 ){  // As Asp.net continues to output to the client  //  Note: (ie kernel browser) requires more than 256 characters (you can output a style in advance to control the subsequent information style)  //  In order to want the client to send new information instantly 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 second (Simulating Complex computing time consumption) Thread. Sleep ( 1000  );}}}} 

The display effect is as follows:

Browser: search for high-speed browsers (compatible mode)-compatible with IE kernel browsers

Effect: display the effect of a number every second-the page is still loading

 

 

Method 2 (not recommended, cause: using the reload render method, you cannot specify relevant information for the server control, such as the value assignment ...):

 Using System;  Using  System. Collections. Generic;  Using  System. Web;  Using  System. Web. UI;  Using  System. Web. UI. webcontrols;  # Region Namespace 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 );  //  Output the style of the following information 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 second (Simulating Complex computing time consumption) Thread. Sleep ( 1000  );}}}} 

The display effect is shown in.

Source code: continuousexport.zip

Author: Zeng qinglei Source: Yun, andArticleThe original text link is clearly displayed on the page; otherwise, the legal liability is retained.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.