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.
- 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.
- public class WebTimer:Control,IPostBackEventHandler
- {
- …………
- IPostBackEventHandler implements#region IPostBackEventHandler implements
- void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
- {
- OnTimer(EventArgs.Empty);
- }
- #endregion
- …………
- }
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:
- private static readonly object EventTimer = new object();
-
- …………
- Events#region Events
- public event EventHandler Timer
- {
- add{Events.AddHandler(EventTimer,value);}
- remove{Events.RemoveHandler(EventTimer,value);}
- }
- #endregion
-
- event handler functions#region event handler functions
- protected virtual void OnTimer(EventArgs e)
- {
- EventHandler timerHandler = (EventHandler)Events[EventTimer];
- if(null != timerHandler)
- {
- timerHandler(this,e);
- }
- }
- #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:
- protected override void OnPreRender(EventArgs e)
- {
- base.OnPreRender(e);
- //call this method to ensure that the _doPostBack method will be called
- Page.ClientScript.GetPostBackEventReference(this,"");
- }
The next step is to compile the JavaScript function and draw relevant actions in the Render function:
- Utility functions#region Utility functions
- private string BuildJavaScript()
- {
- StringBuilder sb = new StringBuilder();
- sb.Append("\n﹤script language=\"javascript\"﹥\n ﹤!-- ");
- sb.AppendFormat("\n setTimeout(\"{0}\",{1});",
- new object[]{this.Page.GetPostBackEventReference(this),interval.ToString()});
- sb.Append("\n //--﹥\n﹤/script﹥");
- return sb.ToString();
- }
- #endregion
-
- protected override void Render(HtmlTextWriter writer)
- {
- /**//*to ensure that this component is in the "runat=server" HtmlForm,
- or there'll be no Post-Back event,as a result that this component will be of no avail
- */
- if(null != Page)
- Page.VerifyRenderingInServerForm(this);
- if(enabled)
- 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.
- Usage of ASP. NET2.0 data source controls
- Analysis on automatic format settings supported during ASP. NET Control Design
- Analysis on the operation list and template editing during ASP. NET Control Design
- Analysis of ASP. NET control designer
- Introduction to ASP. NET Component Design