ASP. NET Component Design-Analysis of ASP. NET Timer

Source: Internet
Author: User

ASP. NET Component Design: ASP. NET Timer I. component requirements

To compile a Timer component on a webpage, you must first solve this problem: how to make the webpage Post back to the Server periodically. Under normal circumstances, this action is triggered by the user clicking a button, but the requirement of the Timer component is automatically triggered at a specific time. For example, the designer sets a Post action every five seconds, the webpage has to make a Post every five seconds. How can this be done? In fact, this can be done through a setTimeout function provided by Javascript, as long as this function is called every 5 seconds by ASP. the _ doPostBack function generated by NET can meet the regular automatic Post requirement. The following is a prototype of the setTimeout function.

 
 
  1. setTimeout(﹤function﹥,﹤interval﹥); 

The first parameter is the function called when the Timeout time arrives, and the second parameter is the length of the Timeout time, in milliseconds millisecond ). After resolving the problem of timed automatic Post on the webpage, the question is how the Server receives this information. The answer is the Post-Back mechanism, the component can get the master control right after automatic Post on the webpage, and trigger the event function mounted by the user.

ASP. NET Component Design: ASP. NET Timer 2. implement Post-Back and draw JavaScript

Now we are faced with two problems: the first is the component base class selection. Because WebTimer is a non-visual component and does not require any appearance attribute, it naturally inherits from the Control class. The second is that WebTimer must implement the Post-Back interface, song can trigger the event function attached to the user after automatic Post on the webpage obtains the master control permission. The following is some program code for WebTimer to process the Post-Back mechanism.

 
 
  1. public class WebTimer:Control,IPostBackEventHandler  
  2. {  
  3.     …………  
  4.     IPostBackEventHandler implements#region IPostBackEventHandler implements  
  5.     void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)  
  6.     {  
  7.         OnTimer(EventArgs.Empty);  
  8.     }  
  9.     #endregion  
  10.     …………  

WebTimer inherits from the Control class and implements the IPostBackEventHandler interface. After the component implements this interface, the Page object will call IPostBackEventHandler registered to participate in the Post-Back operation component one by one when Post-Back occurs. raisePostBackEvent function. Here, the WebTimer component calls the OnTimer function to forward control to the event bound to the designer. The following code is the OnTimer function and event processing part:

 
 
  1. private static readonly object EventTimer = new object();  
  2.  
  3. …………  
  4. Events#region Events  
  5. public event EventHandler Timer  
  6. {  
  7.     add{Events.AddHandler(EventTimer,value);}  
  8.     remove{Events.RemoveHandler(EventTimer,value);}  
  9. }  
  10. #endregion  
  11.  
  12. event handler functions#region event handler functions   
  13. protected virtual void OnTimer(EventArgs e)  
  14. {  
  15.     EventHandler timerHandler = (EventHandler)Events[EventTimer];  
  16.     if(null != timerHandler)  
  17.     {  
  18.         timerHandler(this,e);  
  19.     }  
  20. }  
  21. #endregion 

The next step is to draw JavaScript Functions. Before that, another important thing was to draw the default _ doPostBack function. By default, the Page object does not plot this function every time to save bandwidth. The _ doPostBack function is drawn to the webpage only when a component explicitly calls a function in Page. GetPostBackEventReference or Page. GetPostBackClientHyperlink. Therefore, the WebTimer component must call the Page. GetPostBackEventReference or Page. GetPostBackClientHyperlink function, but where can it be called? Render function? No! At this time, the Page object has entered the drawing state. It is too late to call any function of the Page object at this time, so the best thing is to call the OnPreRender function. The following is the relevant code:

 
 
  1. protected override void OnPreRender(EventArgs e)  
  2. {  
  3.     base.OnPreRender(e);  
  4.     //call this method to ensure that the _doPostBack method will be called   
  5.     Page.ClientScript.GetPostBackEventReference(this,"");  

The next step is to compile the JavaScript function and draw relevant actions in the Render function:

 
 
  1. Utility functions#region Utility functions  
  2. private string BuildJavaScript()  
  3. {  
  4.     StringBuilder sb = new StringBuilder();  
  5.     sb.Append("\n﹤script language=\"javascript\"﹥\n ﹤!-- ");  
  6.     sb.AppendFormat("\n setTimeout(\"{0}\",{1});",  
  7.         new object[]{this.Page.GetPostBackEventReference(this),interval.ToString()});  
  8.     sb.Append("\n //--﹥\n﹤/script﹥");  
  9.     return sb.ToString();  
  10. }  
  11. #endregion  
  12.  
  13. protected override void Render(HtmlTextWriter writer)  
  14. {  
  15.     /**//*to ensure that this component is in the "runat=server" HtmlForm,  
  16.     or there'll be no Post-Back event,as a result that this component will be of no avail  
  17.     */ 
  18.     if(null != Page)  
  19.         Page.VerifyRenderingInServerForm(this);  
  20.     if(enabled)  
  21.         writer.Write(BuildJavaScript());  

In addition to the Render function, two functions are used in the program: Page. the VerifyRenderingInServerForm function is used to confirm that the component is in the HtmlForm control marked as "runat = server". If the component is not in the HtmlForm of this class, there is no Post-Back injury, the WebTimer component naturally does not work. The other is BuildJavaScript, which draws the previously discussed JavaScript function setTimeout with the time set by the user.

One thing to note is that in. net 2.0 contains a warning message: System. web. UI. page. getPostBackEventReference (System. web. UI. control) is out of date. ClientScript is recommended. instead of GetPostBackEventReference, the new function is reloaded four times, but there is no parameter. I still don't know how to change it. I hope I can point it out if I know it!

The basic situation of ASP. NET Timer in ASP. NET component design will be introduced to you here, hoping to help you learn.

  1. Usage of ASP. NET2.0 data source controls
  2. Analysis on automatic format settings supported during ASP. NET Control Design
  3. Analysis on the operation list and template editing during ASP. NET Control Design
  4. Analysis of ASP. NET control designer
  5. Introduction to ASP. NET Component Design

Related Article

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.