ASP.net 2.0 server control's composite control event

Source: Internet
Author: User
Tags definition bool empty execution implement include net variables
Asp.net| Server | control

Inan article above, we discussed the basic theory of creating a composite control and mastered the rendering method of the composite control 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.

· Defines 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.

Using System;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.ComponentModel;
Using System.ComponentModel.Design;
Namespace webcontrollibrary{
public class Compositeevent:compositecontrol {
declaring variables
Private Button _button;
Private TextBox _textbox;
private static readonly Object Eventsubmitkey = new Object ();
Defines the property buttontext that specifies the text on the button
[
Bindable (True), Category ("appearance"), DefaultValue (""), Description ("Get or set the text to display on the button)"
]
public string ButtonText {
get {
EnsureChildControls ();
Return _button. Text;
}
set {
EnsureChildControls ();
_button. Text = value;
}
}
Defines the property text, which represents the input of a text box

[
Bindable (True), Category ("appearance"), DefaultValue (""), Description ("Get or Set text box input text")
]
public string Text {
get {
EnsureChildControls ();
return _textbox.text;
}
set {
EnsureChildControls ();
_textbox.text = value;
}
}
Implementing the event property structure
Public event EventHandler Submit {
Add {
Events.addhandler (Eventsubmitkey, value);
}
Remove {
Events.removehandler (Eventsubmitkey, value);
}
}
Implement OnSubmit
protected virtual void OnSubmit (EventArgs e) {
EventHandler Submithandler = (EventHandler) Events[eventsubmitkey];
if (Submithandler!= null) {
Submithandler (this, e);
}
}
Implementing the event handlers raised by the Submit event
private void _button_click (Object source, EventArgs e) {
OnSubmit (Eventargs.empty);
}
Recreatechildcontrls method to rewrite Icompositecontroldesigneraccessor interface
protected override void Recreatechildcontrols () {
EnsureChildControls ();
}

Overriding the CreateChildControls method to add a child control to a composite control

protected override void CreateChildControls () {
Controls.clear ();
_button = New button ();
_textbox = new TextBox ();
_button.id = "BTN";
_button. Click + + new EventHandler (_button_click);
This. Controls.Add (_button);
This. Controls.Add (_textbox);
}

Overriding the Render method to render other HTML code in the control

protected override void Render (HtmlTextWriter output) {
Output. AddAttribute (Htmltextwriterattribute.border, "0px");
Output. AddAttribute (htmltextwriterattribute.cellpadding, "5px");
Output. AddAttribute (htmltextwriterattribute.cellspacing, "0px");
Output. RenderBeginTag (htmltextwritertag.table);
Output. RenderBeginTag (htmltextwritertag.tr);
Output. RenderBeginTag (HTMLTEXTWRITERTAG.TD);
_textbox.rendercontrol (output);
Output. RenderEndTag ();
Output. RenderBeginTag (HTMLTEXTWRITERTAG.TD);
_button. RenderControl (output);
Output. RenderEndTag ();
Output. RenderEndTag ();
Output. RenderEndTag ();
}
}
}
As shown in the previous code, the composite Control compositeevent contains two properties: Text and ButtonText. The former is used to get or set the text content in the text box, which is used to get or set the display text for the button. In addition, a submit event is implemented in the composite control class. Relevant important logic includes:

First, in the overriding CreateChildControls method, add an event handler _button_click for the child control button.

Second, as with normal custom events, define a top-level event submit for the composite control. This includes defining event attribute structure submit, defining event handler onsubmit.

Third, implement the _button_click event handler, call the event handler onsubmit of the top-level event submit.

The following is the Default.aspx file code created to test the composite control compositeevent.

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<%@ Register tagprefix= "Sample" assembly= "Webcontrollibrary" namespace= "Webcontrollibrary"%>
! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

implement event-include method for composite controls











The example effect is shown in Figure 1.


Figure 1 Effect chart


In the above application, when the user clicks the Submit button, the execution of the Demo1_submit handler is raised, which displays the text box input.

The reader should be aware of its internal execution process. The top-level event defined by the control is submit, and its corresponding event handler is onsubmit, not _button_click. _button_click is the Click event handler for a child control of a composite control. Because the event handler _button_click of the child control is defined in the code implemented by the control, when the user clicks the button, the _button_click is executed first, which requires that the event handler onsubmit for the top-level event submit be invoked. Externally, the event of a child control is exposed as a top-level event.

From the above realization process, the inclusion method uses the code of procedure to implement the event upload function. The bubbling rule described below is different from this, and it uses the. NET Framework provides an event upload mechanism to complete child control event uploads.

   Bubble Method

Bubbling is also known as "event bubbling", and its core is the use of event-uploading mechanisms provided by the ASP.net 2.0 framework. This mechanism allows child controls to propagate events along their containment hierarchies to the appropriate location, and allows the event handlers to be attached to controls on the original control as well as those that expose the bubbling event.

Implementation of the Bubble method, using the two methods specifically used in the control base class for event uploads: OnBubbleEvent and raisebubbleevent. Their declarations are shown below.

OnBubbleEvent method definition
Protected virtual bool OnBubbleEvent (object Source,eventargs args) {return false;}
RaiseBubbleEvent method definition
protected void RaiseBubbleEvent (object Source,eventargs args) {
Control Currenttarget = _parent;
while (Currenttarget!= null) {
if (Currenttarget.onbubbleevent (Source,args) {return;}
Currenttarget = currenttarget.parent;
}
}
The OnBubbleEvent method is used to determine whether the event of a child control is passed up along the composite control hierarchy. In this method, the parameter source represents the event source, and the parameter args represents the EventArgs object that contains the event data. True if the event of the child control is passed up, or false. The default value is False. The RaiseBubbleEvent method is used to assign all event sources and their information to the control's parent and cannot be overridden. Although this method cannot be overridden, the authored control can handle or raise a bubbling event by overriding the OnBubbleEvent method.

The event bubbling of a composite control is mainly in the following two situations:

Case one: The control stops event bubbling and raises and/or handles the event. Raising an event requires calling the method that dispatches the event to the listener. To raise a bubbling event, the control must override OnBubbleEvent to invoke the OnEventName method of the event that raised the bubbling. Controls that raise bubbling events typically expose bubbling events as top-level events. The following code raises a bubbling event.

protected override bool OnBubbleEvent (Object Sender,eventargs e) {
BOOL handled = FALSE;
If (e is CommandEventArgs) {
CommandEventArgs CE = (CommandEventArgs) e;
if (Ce.commandname = = "ButtonClick") {
OnButtonClick (Eventargs.empty);
Handled =true;
}
}
return handled;
}
Case two: The control does some processing and continues to bubble up the event. To do this, the control must override OnBubbleEvent and invoke RaiseBubbleEvent from OnBubbleEvent. The following code bubbles the event after checking the type of the event argument.

protected override bool OnBubbleEvent (Object Sender,eventargs e) {
If (e is CommandEventArgs) {
CommandEventArgs CE = (CommandEventArgs) e;
RaiseBubbleEvent (THIS,CE);
return true;
}
return false;
}
To enable the reader to better understand the bubbling method, the previous section example is implemented using the bubbling method. The source code for the control class is shown below, where the unchanged part is represented by an ellipsis.

Using System;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.ComponentModel;
Using System.ComponentModel.Design;
Namespace webcontrollibrary{
public class Compositeevent:compositecontrol {
declaring variables
Private Button _button;
Private TextBox _textbox;
private static readonly Object Eventsubmitkey = new Object ();
Defines the property buttontext that specifies the text on the button
[
Bindable (True), Category ("appearance"), DefaultValue (""), Description ("Get or set the text to display on the button)"
]
public string ButtonText {...}
Defines the property text, which represents the input of a text box

[
Bindable (True), Category ("appearance"), DefaultValue (""), Description ("Get or Set text box input text")
]

public string Text {...}//Implementation event property structure
Public event EventHandler Submit
{
Add {
Events.addhandler (Eventsubmitkey, value);
}
Remove {
Events.removehandler (Eventsubmitkey, value);
}
}
Implement OnSubmit

protected virtual void OnSubmit (EventArgs e) {
EventHandler Submithandler = (EventHandler) Events[eventsubmitkey];
if (Submithandler!= null) {Submithandler (this, e);}
}
Delete _button_click
Recreatechildcontrls method to rewrite Icompositecontroldesigneraccessor interface
protected override void Recreatechildcontrols () {...}
Overriding the CreateChildControls method to add a child control to a composite control
protected override void CreateChildControls () {
Controls.clear ();
_button = New button ();
_textbox = new TextBox ();
_button.id = "BTN";
_button.commandname = "Submit";
This. Controls.Add (_button);
This. Controls.Add (_textbox);
}
Overriding the OnBubbleEvent method, performing event bubbling
protected override bool OnBubbleEvent (object source, EventArgs e) {
BOOL handled = FALSE;
If (e is CommandEventArgs) {
CommandEventArgs CE = (CommandEventArgs) e;
if (Ce.commandname = "Submit") {
OnSubmit (Eventargs.empty);
handled = TRUE;
}
}
return handled;
}
Overriding the Render method to render other HTML code in the control
protected override void Render (HtmlTextWriter output) {...}
}
}
The Compositeevent class in this example implements the same functionality as the Compositeevent class in the previous section. For control rendering, there is no difference between two classes, mainly in the context of event implementations for composite controls. Difference one: In the Createchildcontrol method of this example, the CommandName property is set for the child control _button, and its property value is submit. Difference Two: Delete the _button_click event handler. Difference three: Overrides the OnBubbleEvent method of the control base class to check whether the event argument is an instance of the CommandEventArgs class. If so, the CommandName member using the event parameter determines whether an event handler onsubmit needs to be raised and returns true.

   Summary

This paper mainly introduces the event implementation method of composite control, and illustrates the application of these methods through a typical example. Overall, it is not particularly difficult to implement events for composite controls. The key is that developers must grasp the essentials of inclusion and bubbling by grasping the implementation of events for common 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.