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.