ASP.net 2.0 server control's composite control event

Source: Internet
Author: User
Tags implement

In the previous article, we discussed the basic theory of creating composite controls and mastered the rendering method of composite controls through a typical application. This article continues to explain the content of creating composite controls, focusing on specific ways to implement events for composite controls.

Introduction to Event handling for composite controls

Talk about the event handling issues of custom controls, as explained in previous series articles. From the foregoing, the core of implementing control events is to define the event property structure and event handlers. However, these elements are the basis for building all custom server controls. It is not possible to implement complex control events by relying on these methods alone. Because a composite control contains child controls, this makes the event handling of the composite control complex. Obviously, the biggest problem that needs to be faced in the event implementation of a composite control is that it does not allow developers to access the child controls directly (although the method that is accessed through the Controls collection can be implemented, the encapsulation of the program is compromised and therefore is disallowed). If the child control's event cannot be raised as a top-level event, the child control's event handling cannot be implemented. Simply put, that is how to implement the child control event upload. Event uploading refers to the event that the control's events are exposed as top-level events, so that the parent control can examine the event and follow the definition to execute the associated event handler.

It is known from the above, the composite Control event processing, mainly to implement the child control event upload process. Here are two common methods of event upload: Inclusion method and bubbling method. The two methods implement different mechanisms, however, the same function is accomplished. In the following sections, we'll start with a theory-and-example approach.

Inclusion method

The core of the inclusion method is to complete the event upload of the child control by calling the top-level event handler for the composite control in the child control's event handler. During execution, when a child control event is raised, the child control's event handler automatically invokes the related top-level event handler.

The key steps of the inclusion method are as follows:

• In the CreateChildControls method, add an event handler for the child control.

• Define top level events and their event handler OnEventName.

• Call OnEventName in the child control's event handler.

• Define the event property structure.

As you know from the above, the steps to include the method are basically similar to the methods used to implement the control described in the previous article. The key is to have one more step to add an event handler for a child control in the CreateChildControls method. For the reader to understand the inclusion method more clearly, an example of implementing an event using the inclusion method for a composite control is given below.

First, using the composite control rendering method described in the previous article, create a composite control consisting of text boxes and buttons, and then upload the button click event to the top-level event, using the inclusion method described above. The source code for the control is listed below.

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.