Vertical cut-in for ASP 3.5 control and component Development notes: High-efficiency Event collection objects

Source: Internet
Author: User

In a few previous examples, the most common definition of event methods, such as those defined in Kingtextbox, is used:
<summary>
For more information on this book, please see:
Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
</summary>
public event EventHandler TextChanged;
protected virtual void ontextchanged (EventArgs e)
{
if (TextChanged! = null)
{
TextChanged (this, e);
}
}
In composite controls, events are generally more common, and if you still define events based on the implementation of the ordinary events above, you will define many event delegate implementation objects, resulting in more memory storage.
In this case, you should save the event through the System.ComponentModel.EventHandlerList collection class, because using this class is more efficient than a common implementation method during the declaration of multiple events. The EventHandlerList class provides a simple list of delegates to add (events.addhandler) and delete (Events.removehandler) delegates, and an object of type EventHandlerList has already been defined in the control base class event s, so you can go directly through base. Events Access Event List object.
The event declaration/invocation portion of the Kingtextbox control is modified using the Events collection Object store event, and in order to preserve the existing functionality of Kingtextbox, a new control example is added, and the control name is called Kingtextboxuseevents. The code for the event section is:
<summary>
For more information on this book, please see:
Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
</summary>

private static readonly Object textchangedkeyobject = new Object ();
Public event EventHandler TextChanged
{
Add
{
Base. Events.addhandler (kingtextboxuseevents.textchangedkeyobject, value);
}
Remove
{
Base. Events.removehandler (Kingtextboxuseevents.textchangedkeyobject,
Value);
}
}
protected virtual void ontextchanged (EventArgs e)
{
EventHandler handler = base. Events[kingtextboxuseevents.textchanged
Keyobject] as EventHandler;
if (handler! = null)
{
Handler (this, e);
}
}
The above code first defines an object textchangedkeyobject that is used as the key for the TextChanged event when the event is stored to the events list, and the corresponding value is the event method body. Next, you define an event delegate type's TextChanged property, which includes an add and a remove clause, which completes the registration and release events function in each of the two clauses. The last OnTextChanged method also slightly changed, adding a bit from base. A code statement that gets the event handle according to the Textchangedkeyobject key in the events list, and if the handle handler is not NULL, causes the developer to register the event.
Control kingtextboxuseevents code is also relatively simple, the following also shows its full code:
<summary>
For more information on this book, please see:
Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
</summary>
[Defaultproperty ("Text")]
[ToolBoxData ("<{0}:kingtextboxuseevents runat=server></{0}:kingtextboxuseevents>")]
public class Kingtextboxuseevents:control, IPostBackDataHandler
{
Public kingtextboxuseevents ()
{
}

<summary>
Set or get display text
</summary>
public string Text
{
Get
{
string s = (string) viewstate["Text"];
Return ((s = = null)? STRING.EMPTY:S);
}

Set
{
viewstate["Text"] = value;
}
}

<summary>
Generate rendering HTML formatting markup
</summary>
<param name= "Writer" ></param>
protected override void Render (HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder ();
Sb. Append ("<input type=/" text/"name=");
Sb. Append ("/" "+ UniqueID +"/"");
Sb. Append ("value=");
Sb. Append ("/" "+ httputility.htmlencode (Text) +"/"");
Sb. Append ("/>");
Writer. Write (sb.) ToString ());
}

Public virtual bool LoadPostData (string Postdatakey, NameValueCollection
Postcollection)
{
string stroldvalue = Text;
String strnewvalue = Postcollection[this. UniqueID];
if (Stroldvalue = = NULL | | (Stroldvalue! = null &&!stroldvalue.equals
(Strnewvalue)))
{
This. Text = Strnewvalue;
return true;
}
return false;
}


public virtual void RaisePostDataChangedEvent ()
{
OnTextChanged (Eventargs.empty);
}

Efficient events
private static readonly Object textchangedkeyobject = new Object ();
Public event EventHandler TextChanged
{
Add
{
Base. Events.addhandler (kingtextboxuseevents.textchangedkeyobject, value);
}
Remove
{
Base. Events.removehandler (kingtextboxuseevents.textchanged
Keyobject, value);
}
}
protected virtual void ontextchanged (EventArgs e)
{
EventHandler handler =base. Events[kingtextboxuseevents.textchanged Keyobject] as EventHandler;
if (handler! = null)
{
Handler (this, e);
}
}
}
The Kingtextboxuseevents control is exactly the same as the Kingtextbox function and applies the same method. In addition, the delegate and parameter objects of the event can be customized, and the next section tells you how to customize your own delegates and event objects.

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.