ASP. NET custom server control internal details series tutorial 1

Source: Internet
Author: User

If you want to reprint it, Please retain your copyright:
/*
* Description: ASP. NET custom server control internal details series tutorial
* Auther: chongchong-innocent haoban
* MSN: chongchong2008@msn.com
* Dates: 2007-05-20
* Copyright: chongchong2008 Yichang Hubei China
*/

About custom ASP. NET custom server controlsArticleThere are already many examples of source code on the Internet, but some internal sections make it hard for some friends who first came into contact with this technology to understand and have doubts about all aspects of server controls. For this reason, I took some time to sort out what I understood so that you can exchange and learn.

 

1. Understand event delegation, events, events triggered, and event Optimization

Naming Conventions for event delegation and event data in the. NET Framework:
The event data class consists of the event name and the suffix eventargs, for example, sxlogineventargs.
Event delegation consists of the event name and suffix eventhandler, for example, sxlogineventhandler.
The method for triggering an event is to add the prefix on and onsxlogin before the event.

1. Event Delegate
The delegate is actually a class. The delegate has the granularity of the function pointer and the security of the interface. Why is it that the delegate is a secure function pointer used for callback methods, the signature of the method must match the signature of the delegate.

Let's take a look at the delegation's reputation:
Public Delegate void sxlogineventhandler (Object sender, sxlogineventargs E );

The preceding definition defines an event Delegate for sxlogineventhandler. The return type is void and one object and sxlogineventargs parameters are accepted respectively. Object indicates the event sender, and E indicates the event data.

We can see that the name and class of event Delegate are similar. You only need to add the keyword delegate.
The delegate is indirectly derived from system. Delegate and directly derived from system. multicastdelegate.

2. Event
An event is a message or notification sent by the class when an action occurs or the status changes.

Let's take a look at the event's reputation:
Public event sxlogineventhandler sxlogin;
We can see that the event is associated with a delegate.

3. Events
To implement events in a class, an event data class, event delegation, and a method for releasing Event Notifications are required. We need to combine these together. The following is an example:

3.1 If the class does not have any associated event data, the eventargs class or other existing event data classes must be used directly. Otherwise, you need to define an event data class, which must be derived from system. eventargs, as follows:
Public class sxlogineventargs: eventargs {...}

3.2 If the event has no associated data, system. eventhandler is directly used as the event Delegate. Otherwise, an event Delegate must be set as follows:
Public Delegate void sxlogineventhandler (Object sender, sxlogineventargs E );

3.3 define event members with the event keyword, followed by the event Delegate corresponding to this, as follows:
public event sxlogineventhandler sxlogin;
3.4 define a virtual method call event Delegate in the class. The method name is prefixed with on before the event, as shown below:
protected virtual void onsxlogin (sxlogineventargs e)
{< br> If (sxlogin! = NULL)
{< br> sxlogin (this, e);
}< BR >}

4. Event Optimization
If a class triggers multiple events, it is inefficient to declare an event field member for each event. For this reason, we use another method to define events using attributes.
The. NET Framework has a system. componentmodel. eventhanlderlist class, which is an optimized delegated storage and retrieved linked list. The following describes how to optimize the Event Mode:
Private eventhanlderlist events;
Protected eventhanlderlist events
{
Get
{
If (events = NULL)
{
Events = new eventhanlderlist ();
}
Return events;
}
}

Protected static readonly object sxlogineventobject = new object ();

we use properties rather than fields to define events, as shown below:
public event sxlogineventhandler sxlogin
{< br> Add {events. addhandler (sxlogineventobject, value) };
remove {events. removehandler (sxlogineventobject, value) };< BR >}

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.