Event handling of ASP. NET components and developing composite controls

Source: Internet
Author: User

Event Processing for composite controls

This article is excerpted from the book Ding Jie Niu: vertically cutting ASP. NET 3.5 controls and component development technology

 

The event processing function of the composite control can be divided into three types:
(1) Internal event handling of the neutron control of the composite control.
(2) contains processing mechanisms-subcontrol events that call the event instance delegated by the primary control.
(3) bubble processing mechanism.
The next three sections illustrate how these three event processing methods are implemented.
5.3.3.1 internal event handling of neutron controls in composite controls
This is the simplest processing method. The subcontrol events in the composite controls exist independently and have nothing to do with the primary controls. They also complete their own functions, which is easy to use, just like using events on pages.
This section uses the calculatorcontrol control as an example to demonstrate how to use this type of event. First, let's take a look at the running of the control, as shown in Figure 5-4.
This is a standard composite control, all composed of child controls. There are two textbox representing the operation operations. There are four buttons below to perform addition, subtraction, multiplication, division respectively. The source code is as follows:
/// <Summary>
/// For more information about this book, see:
// Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </Summary>
[Toolboxdata ("<{0}: calculatorcontrol runat = Server> </{0}: calculatorcontrol>")]
Public class calculatorcontrol: compositecontrol
{
// Operands
Private textbox tb1;
Private textbox tb2;

// Display the result
Private Label Lb;

// Operation (+ -*/)
Private button Bt1;
Private button bt2;
Private button bt3;
Private button bt4;

Private const string resulttext = "result list :";
Private unit buttonwidth = unit. pixel (30 );

Protected override void createchildcontrols ()
{
Tb1 = new Textbox ();
Tb1.id = "textbox1 ";
This. Controls. Add (tb1 );

Tb2 = new Textbox ();
Tb2.id = "textbox2 ";
This. Controls. Add (tb2 );

Lb = new label ();
LB. ID = "label1 ";

LB. Text = resulttext;
This. Controls. Add (LB );

Bt1 = new button ();
Bt1.id = "button1 ";
Bt1.width = buttonwidth;
Bt1.text = "+ ";
Bt1.commandargument = "+ ";
Bt1.click + = new eventhandler (bt_click );
This. Controls. Add (Bt1 );
Bt2 = new button ();
Bt2.id = "button2 ";
Bt2.width = buttonwidth;
Bt2.text = "-";
Bt2.commandargument = "-";
Bt2.click + = new eventhandler (bt_click );
This. Controls. Add (bt2 );
Bt3 = new button ();
Bt3.id = "button3 ";
Bt3.width = buttonwidth;
Bt3.text = "*";
Bt3.commandargument = "*";
Bt3.click + = new eventhandler (bt_click );
This. Controls. Add (bt3 );
Bt4 = new button ();
Bt4.id = "button4 ";
Bt4.width = buttonwidth;
Bt4.text = "/";
Bt4.commandargument = "/";
Bt4.click + = new eventhandler (bt_click );
This. Controls. Add (bt4 );
}

Void bt_click (Object sender, eventargs E)
{
Try
{
If (resulttext! = LB. Text)
{
LB. Text = LB. Text + ",";
}
Switch (button) sender). commandargument)
{
Case "+": lb. Text = LB. Text + convert. tostring (convert. toint32 (this. tb1.text) + convert. toint32 (this. tb2.text); break;
Case "-": lb. Text = LB. Text + convert. tostring (convert. toint32 (this. tb1.text)-convert. toint32 (this. tb2.text); break;
Case "*": lb. Text = LB. Text + convert. tostring (convert. toint32 (this. tb1.text) * convert. toint32 (this. tb2.text); break;
Case "/": lb. Text = LB. Text + convert. tostring (convert. toint32 (this. tb1.text)/convert. toint32 (this. tb2.text); break;
}
}
Catch
{
LB. Text = "It's not right format, please input again .";
}
}

Protected override void render (htmltextwriter writer)
{
Writer. addattribute (htmltextwriterattribute. style, "padding: 10;
Background-color: # c0c0fe; font-size: 12px; width: 180px;
Height: 160; Vertical-align: Top; text-align: center ;");
Writer. renderbegintag (htmltextwritertag. Div );

Writer. addattribute (htmltextwriterattribute. Border, "0 ");
Writer. addattriding (htmltextwriterattribute. cellpadding, "0 ");
Writer. addattriing (htmltextwriterattribute. cellspacing, "0 ");
Writer. addattriign (htmltextwriterattribute. valign, "Middle ");
Writer. renderbegintag (htmltextwritertag. Table );

// Operating Item 1
Writer. renderbegintag (htmltextwritertag. tr );
Writer. renderbegintag (htmltextwritertag. TD );
Tb1.rendercontrol (writer );
Writer. renderendtag ();
Writer. renderendtag ();

// <Br>
Writer. writebreak ();

// Operating symbol
Writer. renderbegintag (htmltextwritertag. tr );
Writer. addattribute (htmltextwriterattribute. Align, "Left ");
Writer. renderbegintag (htmltextwritertag. TD );
Writer. writeencodedtext ("+ -*/");
Writer. renderendtag ();
Writer. renderendtag ();

// Operating item2
Writer. renderbegintag (htmltextwritertag. tr );
Writer. renderbegintag (htmltextwritertag. TD );
Tb2.rendercontrol (writer );
Writer. renderendtag ();
Writer. renderendtag ();

// Operating symbol
Writer. renderbegintag (htmltextwritertag. tr );
Writer. addattribute (htmltextwriterattribute. Align, "Left ");
Writer. renderbegintag (htmltextwritertag. TD );
Writer. writeencodedtext ("Equal ");
Writer. renderendtag ();
Writer. renderendtag ();

// The relust label
Writer. renderbegintag (htmltextwritertag. tr );
Writer. addattribute (htmltextwriterattribute. Align, "Left ");
Writer. renderbegintag (htmltextwritertag. TD );
LB. rendercontrol (writer );
Writer. renderendtag ();
Writer. renderendtag ();

// Button1
Writer. renderbegintag (htmltextwritertag. tr );
Writer. renderbegintag (htmltextwritertag. nobr );
Writer. renderbegintag (htmltextwritertag. TD );
Bt1.rendercontrol (writer );
Bt2.rendercontrol (writer );
Bt3.rendercontrol (writer );
Bt4.rendercontrol (writer );
Writer. renderendtag ();
Writer. renderendtag ();

Writer. renderbegintag (htmltextwritertag. tr );
Writer. addattribute (htmltextwriterattribute. Height, "10px ");
Writer. renderbegintag( htmltextwritertag. TD );
Writer. renderendtag ();
Writer. renderendtag ();

Writer. renderendtag ();

Writer. renderendtag ();
}
}
The Code logic is also relatively simple. The event registration of operation buttons adopts the standard event format:
Bt1.click + = new eventhandler (bt_click );
Void bt_click (Object sender, eventargs E)
{
//... ...
}
Unlike the past, event registration is not used in the page control as usual, but in our custom control.
In addition, because the events of the four buttons are similar, let them share an event instance bt_click. In the event Method body, use the commandargument attribute of the button to distinguish which button is clicked and decide which operation to execute.
The above events are defined within the control. subcontrols register and reference events, that is, the registration and call of events are completely encapsulated within the control, which is not interactive to developers, such events are often used in development.
However, when a composite control is triggered by an event, developers often want to do their own thing. In this case, they need to hand over the event to the master control and expose the event in a unified manner by the Master Control, in this way, developers only need to register events for the primary control when using the control. The remaining primary control is responsible for triggering subcontrol events or executing some functions of the subcontrol, the event cohesion between the main control and its subcontrols is involved here, which is generally divided into two processing methods: the inclusion method and the bubble method. The following two sections describe these two methods.

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.