Implementation events for ASP.NET2.0 server control development

Source: Internet
Author: User
Tags final

The previous articles explain what to do with using ASP.net 2.0 technology to create custom server control properties. Starting with this article, including a few subsequent articles, explores ways to create custom server control events. This article focuses on the basic concepts of implementing control events that are important to help developers create events for server controls.

1. Basic concepts of events

An event is a message or notification given by a class when an action occurs or changes in state. Typically, the occurrence or change of a state is initialized by a user-interface action, for example, by clicking a button, or because of other program logic. The class that generates the event or the class that sends the notification is called the event source sender, and the class that receives the event is called the event receiver receiver. The association is implemented through a delegate (delegate). A common Application event code is listed below.

// 声明事件
ClickcustomControl.Click += new EventHandler(this.customControl1_Clicked);
// 实现事件处理程序
customControl1_Clicked(object sender,EventArgs e){......}

The code above enumerates the procedures for declaring events and implementing event handlers for a server control. Since the process is very simple, there will be little explanation here. In addition, in practical applications, developers can implement event mechanisms for server controls by not using the above declaration event approach, but simply listing "OnClick = customcontrol1_clicked" 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 event mechanisms for controls is not an easy task.

From the perspective of server control development, control events, which refer only to server-side events and not to client events, can come from two aspects: one is an event inherited from a base class. For example, if a custom control inherits from the Button class, the control inherits the click event of the base class. The second is a custom event that is created based on development requirements. Each of these events is described below.

2. Implementing events inherited from a base class

As we all know, custom server controls are ultimately derived from System.Web.UI.Control. Some events have already been defined in the base class. Therefore, during the creation of a server control, it is likely that you will need to override the following inherited multiple events.

· DataBinding Event: This event occurs when a server control is bound to a data source, and its corresponding event handler is ondatabinding.

· Disposed Event: This event occurs when a server control resource is freed from memory and its corresponding event handler is ondisposed. This is the final stage of the server control lifecycle.

· Init Event: This event occurs when the server control initializes, and its corresponding event handler is OnInit. The Init event is the first step in the life cycle of the control.

· Load event: This event occurs when the 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, before rendering, and its corresponding event handler is OnPreRender.

· Unload Event: This event occurs when a server control is unloaded from memory, and its corresponding event handler is onunload.

The above is a brief description of several events for the control base class. Because server controls inherit from the control base class (WebControl is also inherited from control classes), developers can completely override event handlers for events so that they can implement some customization.

To implement custom inherited events, you need to override the protected OnEventName method inherited from the base class without attaching a delegate (EventHandler). In general, an overridden event handler should call the OnEventName method of the base class to ensure that the delegate attached to the event is invoked (unless the delegates are not invoked). The following code fragment illustrates the processing of the custom control overriding inherited databinding events.

protected override void OnDataBinding(EventArgs e)
{
 //添加一些自定义逻辑代码
 //调用基类方法
 base.OnDataBinding(e);
}

As shown in the code above, in overriding the event handler ondatabinding, you first need to add some custom logic code that is implemented based on the application requirements, and then be sure to remember to call the base class method.

The above describes the process of overriding the corresponding event handlers for events and derived classes of the control base class. It is not noted above that custom server controls can override only the above several event handlers from the control base class event. If a custom control inherits from other base classes that originally had events, such as Button, DataList, and so on (in the final analysis, they also inherit from the control base class), the inherited event handler can still be overridden, for example, Controls that inherit from the button class naturally obtain the click event and can override the OnClick event handler.

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.