Implement the bubble Processing Mechanism in ASP. NET composite components

Source: Internet
Author: User

Implement the bubble Processing Mechanism in ASP. NET composite components

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

 

In composite controls, if the structures between child controls are complex and in many cases multi-level structures, such as the template container control in the gridview main control, the template container control contains the command button control and many controls. In this case, it is troublesome to use the previously mentioned event mechanism, and the code looks messy, because you need to register an event for each button (the number of buttons in the button column of the gridview depends on the number of records on each page and the number of buttons in each row ).
Based on this, the ASP. NET Framework provides the bubbling method, that is, events are bubbling up. The core of the framework is to use the event upload mechanism provided by. netframework. This mechanism allows the child control to spread events up along its inclusive hierarchy, rather than triggering events with each command button. During the event upload process, we determine when to trigger a custom event. By responding to this event, you do not have to write the event processing methods for the child controls separately.
The implementation of the bubble method is mainly to use the two methods onbubbleevent and raisebubbleevent used for event upload in the control base class. The original declaration code of the two methods in the base class control is as follows:
/// <Summary>
/// For more information about this book, see:
// Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </Summary>
Protected virtual bool onbubbleevent (Object source, eventargs ARGs)
{
Return false;
}

Protected void raisebubbleevent (Object source, eventargs ARGs)
{
For (control = This. parent; control! = NULL; control = control. Parent)
{
If (control. onbubbleevent (source, argS ))
{
Return;
}
}
}
The onbubbleevent method is used to trigger custom events. by returning a Boolean value, it indicates whether the events of the subcontrol continue to be passed up along the composite control hierarchy. The source parameter indicates the event source (such as button), and The args parameter indicates the eventargs object that contains event data (when the commandname of the button is specified, the system will automatically create its commandeventargs object, the base class of this object type is eventargs ). If you want to process the current container control bubble event, you need to override the onbubbleevent event to trigger your own event in the onbubbleevent rewriting method. After processing your event logic, a Boolean value is returned, to determine whether to let the bubble Mechanism continue to pass up the container structure. The default value of the control-based virtual method is false.
The raisebubbleevent method is used to allocate all event sources and their information to the control's parent level. It is very important that it cannot be overwritten and can be called directly when it is called.
The following uses the Bubble Method to trigger the searchcontrol button event and name the control searchcontrolbubbleup. The implementation is very simple. Because the only difference in the method of triggering an event is that you only need to modify the Code related to the event for the changed part, namely:
...
Btnsearch. Click + = new eventhandler (btnsearch_click );
...
Protected virtual void onbuttonsearchclick (searcheventargs E)
{
Searcheventhandler buttonsearchclickhandler = (searcheventhandler) events [buttonsearchclickobject];
If (buttonsearchclickhandler! = NULL)
{
Buttonsearchclickhandler (this, e );
}
}
...
To:
...
Btnsearch. commandname = "buttonsearchclick ";
...
Protected override bool onbubbleevent (Object sender, eventargs E)
{
Bool handled = false;
If (E is commandeventargs)
{
Commandeventargs Ce = (commandeventargs) E;
If (Ce. commandname = "buttonsearchclick ")
{
Searcheventargs ARGs = new searcheventargs ();
Args. searchvalue = This. text;
Onbuttonsearchclick (ARGs );
Handled = true;
}
}
This. raisebubbleevent (sender, e );
Return handled;
}
...
In the above Code, the original searchcontrol control is completed by registering the standard event of the button, and the example control searchcontrolbubbleup in this section only performs two steps:
Specifies the commandname attribute of the button control. This attribute is usually used in combination with commandargument.
Override the onbubbleevent method. In this method, first obtain the parameter object, and then judge whether the currently triggered event source is the button we specified based on the parameter object (whether the commandname attribute is equal to "buttonsearch click "). If the button is specified, the onbuttonsearchclick (ARGs) event that calls the main control is triggered, and the handled variable indicating the return value is set to true. The raisebulleevent method of the control base class is called before the final return value. The event source sender and E parameter objects are sent to the control parent class.
The complete source code of the searchcontrolbubbleup control is as follows:
/// <Summary>
/// For more information about this book, see:
// Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </Summary>
[Defaultproperty ("text")]
[Defaultevent ("buttonsearchclick")]
[Toolboxdata ("<{0}: searchcontrolbubbleup runat = Server> </{0}: searchcontrolbubbleup>")]
Public class searchcontrolbubbleup: compositecontrol
{
Private button btnsearch;
Private textbox tbsearchtext;

[Category ("Search")]
[Defaultvalue ("")]
[Description ("Get text box value")]
Public String text
{
Get
{
This. ensurechildcontrols ();
Return tbsearchtext. text;
}
}

Private Static readonly object buttonsearchclickobject = new object ();
Public event searcheventhandler buttonsearchclick
{
Add
{
Base. Events. addhandler (buttonsearchclickobject, value );
}
Remove
{
Base. Events. removehandler (buttonsearchclickobject, value );
}
}

Protected override void createchildcontrols ()
{
This. Controls. Clear ();
Btnsearch = new button ();
Btnsearch. ID = "BTN ";
Btnsearch. Text = "Search ";
Btnsearch. commandname = "buttonsearchclick ";

Tbsearchtext = new Textbox ();
Tbsearchtext. ID = "TB ";
This. Controls. Add (btnsearch );
This. Controls. Add (tbsearchtext );
}

Protected virtual void onbuttonsearchclick (searcheventargs E)
{
Searcheventhandler buttonsearchclickhandler = (searcheventhandler)
Events [buttonsearchclickobject];
If (buttonsearchclickhandler! = NULL)
{
Buttonsearchclickhandler (this, e );
}
}

Protected override bool onbubbleevent (Object sender, eventargs E)
{
Bool handled = false;
If (E is commandeventargs)
{
Commandeventargs Ce = (commandeventargs) E;
If (Ce. commandname = "buttonsearchclick ")
{
Searcheventargs ARGs = new searcheventargs ();
Args. searchvalue = This. text;
Onbuttonsearchclick (ARGs );
Handled = true;
}
}
This. raisebubbleevent (sender, e );
Return handled;
}

Protected override void render (htmltextwriter output)
{
Output. addattriter( htmltextwriterattribute. Border, "0px ");
Output. addattriding (htmltextwriterattribute. cellpadding, "5px ");
Output. addattriing (htmltextwriterattribute. cellspacing, "0px ");
Output. renderbegintag (htmltextwritertag. Table );
Output. renderbegintag (htmltextwritertag. tr );
Output. renderbegintag (htmltextwritertag. TD );
Tbsearchtext. rendercontrol (output );
Output. renderendtag ();
Output. renderbegintag (htmltextwritertag. TD );
Btnsearch. rendercontrol (output );
Output. renderendtag ();
Output. renderendtag ();
Output. renderendtag ();
}
}
The use of the searchcontrolbubbleup control on the page is exactly the same as that of searchcontrol.
When using the bubble mechanism, pay attention to the control hierarchy. When developing list controls and other complex controls, for example, this mechanism is often used to process all edit and DELETE command columns.
This control can now complete basic search functions, but it still lacks a more intelligent function. In the next section, we will improve the control and complete its final version.

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.