Development of ASP. NET Server controls

Source: Internet
Author: User

This article describes how to create a custom ASP. NET Server Control event. This article focuses on the basic concepts of implementing control events. These concepts are of great significance for developers to create events for ASP. NET Server controls.

1. Basic concepts of events

An event is a message or notification sent by the class when an action occurs or the status changes. Generally, status changes are initiated by user interface actions, such as clicking a button or other program logic. The class that generates the event or sends the notification is called the event source sender, and the class that receives the event is called the event receiver. The two are associated by entrusting delegate. The following describes a common application Event code.

 
 
  1. // Declare the event 
  2. ClickcustomControl. Click + =NewEventHandler (This. Customcontrolpolicclicked );
  3. // Implement the event handler 
  4. Customcontrolpolicclicked (ObjectSender, EventArgs e ){......}

The above Code lists the process of declaring events and implementing event handlers for ASP. NET Server controls. This process is very simple and I will not explain it here. In addition, in practical applications, developers can implement the event mechanism for server controls without using the above event declaration methods, instead, just list "OnClick = customcontrolpolicclicked" in the control declaration tag. In fact, the declaration of events and the implementation of specific event handlers are relatively simple and easy to use. However, implementing the event mechanism for controls is not easy.

From the perspective of ASP. NET Server Control development, control events only refer to server-side events, but not client events. These events may come from two aspects: first, events inherited from the base class. For example, if a custom control inherits from the Button class, the control inherits the Click Event of the base class. Second, custom events created based on development needs. The two events are described below.

2. Events inherited from the base class

As we all know, custom ASP. NET Server controls are derived from System. Web. UI. Control. Some events have been defined in the base class. Therefore, when creating an ASP. NET Server Control, you may need to override the following inherited events.

· DataBinding event: When the ASP. NET Server Control is bound to the data source, the corresponding event handler is OnDataBinding.

· Disposed event: When a server control resource is released from memory, the corresponding event handler is OnDisposed. This is the final stage of the server control lifecycle.

· Init event: When the ASP. NET Server Control is initialized, the corresponding event handler is OnInit. The Init event is the first step in the control lifecycle.

· Load event: This event occurs when the ASP. NET Server Control is loaded into the Page object, and its corresponding event handler is OnLoad.

· PreRender event: This event occurs after the Control object is loaded and before it is rendered. The corresponding event handler is OnPreRender.

· Unload event: When the ASP. NET Server Control is detached from the memory, the corresponding event handler is OnUnload.

The above content briefly describes several events of the Control base class. Because the server controls are inherited from the Control base class WebControl and also from the Control class), developers can completely rewrite the event handler corresponding to the event, in this way, you can implement some custom content.

To implement custom inherited events, you need to override the protected OnEventName method inherited from the base class, instead of entrusting EventHandler ). Generally, the overwritten event handler should call the OnEventName method of the base class to ensure that the call is attached to the event Delegate unless you do not want to call these delegates ). The following code snippets describe the process of overwriting the inherited DataBinding event of a custom control.

 
 
  1. Protected Override VoidOnDataBinding (EventArgs e)
  2. {
  3.  // Add custom logic code 
  4.  // Call the base class Method 
  5.  Base. OnDataBinding (e );
  6. }

As shown in the above Code, in the process of rewriting the event handler OnDataBinding, you first need to add some custom logic code based on application requirements. Then, remember to call the basic class method.

The above content introduces the process of rewriting the corresponding event handler for the Control-based events and derived classes. It should be noted that the preceding section does not indicate that the custom server Control can only rewrite the preceding event handlers for Control-based events. If custom controls inherit from other base classes with events, such as buttons and DataList, they also inherit from the Control base class ), the inherited event handler can still be overwritten. For example, the control inherited from the Button class naturally obtains the Click event, and The OnClick event handler can be rewritten.

3. Create a custom ASP. NET Server Control event

Before introducing how to create a custom Server Control event, let's briefly review the relevant event model.

On the Web form page. events associated with the. NET Server Control are triggered by the client and handled by the Web server. Note: events must be called "triggers" instead of "triggers" and "triggers, they are both inaccurate and nonstandard ). For events triggered by ASP. NET Server controls on the client, the ASP. NET event model collects request information and uses HTTP Post to transmit detailed information to the server. The Page Framework on the server interprets the announcement to determine the event that occurred, and then calls the appropriate processing method. 1. This process is briefly described.

 

1. on the client computer, the user clicks Add in the shopping cart to Add the selected item to the shopping cart. After clicking, the event model collects relevant information, such as Submit = btnAddToCart, Prod3 = Gizmo, and so on, and transmits the information to the server through Post. After receiving this information, the server first analyzes it and then calls the event handler btnAddToCart (obj, event) for processing. The above is the basic event processing model.

For general application developers, they only need to implement the event handler of the Control. Further information is hidden for them, and there is no need to care more. However, as an ASP. NET Server Control developer, you must carefully consider this event processing model.

If you think carefully about the above process, you will find two important issues to be addressed in the event processing model. First, how does the server capture the return Click Event? Second, how does it process the data uploaded back to the server through Post. These two questions are crucial. If you can solve these two problems, it is very easy to create custom ASP. NET Server control events.

To solve the preceding problems, ASP. NET provides two important interfaces: IPostBackEventHandler and IPostBackDataHandler. The IPostBackEventHandler interface is used to process page return events triggered by the client. To implement this interface, the ASP. NET Server Control can correspond the client's form submission event to the server-side event, and process the client event through the event processing program. The IPostBackDataHandler interface is used to check the data submitted to the page and determine whether the data has been modified on the client. When the control implements this interface, the control automatically has the ability to participate in data return processing. Developers can implement interface-related members to complete the processing logic for the returned data.

In fact, the vast majority of server controls in ASP. NET trigger the return from the client to the server, and many server controls implemented by the reader must also trigger the return. Therefore, the above two interfaces are very important for implementing control events. This section briefly introduces them. In the subsequent articles, the reader will take a typical example to learn more about how to implement interface members, capture callback events, and process returned data.

In addition, ASP. NET enhances callback processing functions. For example, use the System. Web. UI. ICallbackEventHandler interface and the Page. GetCallbackEventReference method. Applications of these objects can run server code on the client to avoid losing the client status and avoid overhead of server round-trip processing. These contents have some relationships with server control events. However, callback applications are rarely used in ASP. NET Server controls. Therefore, there will be no more instructions.

4. Summary

From the perspective of technological development, ASP. NET has been upgraded from 1.x to version 2.0 without any significant changes in ASP. NET Server Control event development. If you have learned about how to create a server control event under ASP. NET 1.x, you can develop the event based on the methods and ideas of 1.x in the past. In the following article, I will introduce the implementation of ASP. NET Server controls to capture callback events through typical examples.

  1. ASP. NET control development tips: disable unnecessary functions of the base class
  2. Analysis on the use of HtmlTextWriter class in ASP. NET control development skills
  3. Analysis on ComboBox display of ASP. NET control development skills
  4. Analysis of Custom Controls Based on ASP. NET control development
  5. Analysis on the use of the Render method for ASP. NET Server controls

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.