Asp. NET to customize your own delegate and event argument classes

Source: Internet
Author: User

This note is selected from the Discovering: vertical entry to ASP 3.5 control and component development technology


    generally in real development, for events that do not need to pass data information, like the events of the Kingtextbox control above, the arguments passed when the event is raised are Eventargs.empty, as follows:
OnTextChanged (Eventargs.empty);
    This is because the TextChanged event of the control Kingtextbox is relatively simple, and there is no need for parameter objects to pass data. But like some complex controls such as the GridView button command event, you have to have a parameter command to indicate which button was clicked; The page turn event requires the EventArgs parameter object to pass the current page information to the second parameter of the event body of the background code The developer then obtains the corresponding page data from the database based on this page parameter, as well as the ItemDataBound event, which also needs to pass data such as the current row information and index as the EventArgs parameter into the event instance.
When these conditions are met, we need to define our own event argument classes and delegates instead of using the default System.EventArgs class. Here's how to define the event arguments class and delegate using the grid control's paging function, see the following code:
//<summary>
//
/ </summary>
Public delegate void Gridpagechangedeventhandler (object source, Gridpage Changedeventargs e);
public class GridPageChangedEventArgs:System.EventArgs
{
    public Gridpagechangedeventargs ( )
    {
   }

private int intcurrentpageindex;
Public new int CurrentPageIndex
{
get {return intcurrentpageindex;}
set {Intcurrentpageindex = value;}
}

private int intpagecount;
Public new int PageCount
{
get {return intpagecount;}
set {Intpagecount = value;}
}

private int intpagesize;
Public new int PageSize
{
get {return intpagesize;}
set {intpagesize = value;}
}
}
The above code defines a parameter class that stores paging related information, which is derived from the System.EventArgs class, where the system does not enforce the requirement to inherit from the class, but it is also possible to inherit from the class, but there is a benefit to inheriting it. First look at the code for the System.EventArgs base class:
<summary>
///
</summary>
public class EventArgs
{
An event that represents no event data
public static readonly EventArgs Empty;
Initializes a new instance of the System.EventArgs class
public EventArgs ();
}
EventArgs In addition to a constructor method, there is an Empty property of its own type, from which you can know the front in the ontextchanged (Eventargs.empty) when it is invoked; The purpose of passing eventargs.empty as an empty parameter when an event is raised. This is to illustrate that if our Gridpagechangedeventargs class is inherited from EventArgs, it is not only possible to pass Gridpagechangedeventargs class objects, You can also pass an empty parameter object in the form of Gridpagechangedeventargs.empty.
Some attributes are defined in the Gridpagechangedeventargs method body, representing the current page (CurrentPageIndex), the total number of pages (PageCount), and the page size (PageSize). These are our custom page information data.
This sentence in the code:
public delegate void Gridpagechangedeventhandler (object source, Gridpage Changedeventargs e);
Defines a delegate gridpagechangedeventhandler. The delegate can specify an event method such that the first argument is the object type, and the second parameter is the page parameter class object that we defined above gridpagechanged EventArgs. When registering an event, the delegate guarantees that the two parameter types of the event body that are automatically generated in the code behind the page are consistent with their own two parameter types. The following is the event daemon code that is registered on the page:
protected void Grid1_pageindexchanged (Object Source,gridpagechangedeventargs e)
{
int intcurrentpageindex = E.currentpageindex;
int intpagesize = e.pagesize;
Get Data logic
}
You can see that the second parameter type is the Griddocpagechangedeventargs type that we define, and in the event method body, you can go directly through E. CurrentPageIndex and E.pagesize get the data, and that's a lot more.
Now that the page parameter objects and delegates are defined, here's how they are applied inside the main control. Declare the event code as follows:
<summary>
///
</summary>
Private new Static readonly object eventpageindexchanged = new Object ();
[Category ("Action"), Description ("Flip event")]
Public event Gridpagechangedeventhandler PageIndexChanged
{
Add
{
Base. Events.addhandler (grid.eventpageindexchanged, value);
}
Remove
{
Base. Events.removehandler (grid.eventpageindexchanged, value);
}
}
This continues with the High Efficiency Event Collection list Object Base.events in 5.3.1, where the name of the event is pageindexchanged and the delegate type is Gridpagechangedeventhandler the delegate type we defined earlier.
The core code that raises the event is as follows:
<summary>
///
</summary>
protected override bool OnBubbleEvent (object source, EventArgs e)
{
BOOL handled = FALSE;
If (e is Gridcommandeventargs)
{
if (((Gridcommandeventargs) (e)). CommandSource) is LinkButton)
{
LinkButton lb= ((LinkButton) (((Gridcommandeventargs) (e)). Command Source));
if (Lb.commandname = = "Page")
{
if (lb.commandargument = = "Buttonfirst")
{
Gridpagechangedeventargs ee = new Gridpagechangedeventargs ();
if (this. CurrentPageIndex! = 0)
{
This. CurrentPageIndex = 0;
Ee. CurrentPageIndex = this. CurrentPageIndex;
Ee. PageCount = this. PageCount;
Ee. PageSize = this. PageSize;
This. Onpageindexchanged (EE);
}
handled = TRUE;
}

if (lb.commandargument = = "Buttonnext")
{
Gridpagechangedeventargs ee = new Gridpagechangedeventargs ();
if (this. CurrentPageIndex < this. PAGECOUNT-1)
{
This. CurrentPageIndex + = 1;
Ee. CurrentPageIndex = this. CurrentPageIndex;
Ee. PageCount = this. PageCount;
Ee. PageSize = this. PageSize;
This. Onpageindexchanged (EE);
}
handled = TRUE;
}

if (lb.commandargument = = "Buttonprev")
{
Gridpagechangedeventargs ee = new Gridpagechangedeventargs ();
if (this. CurrentPageIndex > 0)
{
This. CurrentPageIndex-= 1;
Ee. CurrentPageIndex = this. CurrentPageIndex;
Ee. PageCount = this. PageCount;
Ee. PageSize = this. PageSize;
This. Onpageindexchanged (EE);
}
handled = TRUE;
}

if (lb.commandargument = = "Buttonlast")
{
Gridpagechangedeventargs ee = new Gridpagechangedeventargs ();
if (this. CurrentPageIndex! = this. PAGECOUNT-1)
{
This. CurrentPageIndex = this. PageCount-1;
Ee. CurrentPageIndex = this. CurrentPageIndex;
Ee. PageCount = this. PageCount;
Ee. PageSize = this. PageSize;
This. Onpageindexchanged (EE);
}
handled = TRUE;
}
}
}
}
return handled | | Base. OnBubbleEvent (source, E);
}
The above onbubbleevent methods are mainly used in composite controls, handling child control events in bubbling form, followed by a composite control bubbling handling event mechanism, which is explained in detail. In addition, in the control's page bar pre-placed four paging function buttons, respectively, "Home", "previous page", "Next Page", "Last", and set their properties CommandName are "page", CommandArgument respectively "Buttonfirst "," Buttonprev "," Buttonnext "," Buttonlast ".
This allows you to determine what logic to perform based on the command and parameters of the button. Just take the button "next page" (command= "page" &&commandargument= "Buttonnext") as an example to explain the code logic:
<summary>
///
</summary>
if (lb.commandargument = = "Buttonnext")
{
Gridpagechangedeventargs ee = new Gridpagechangedeventargs ();
if (this. CurrentPageIndex < this. PAGECOUNT-1)
{
This. CurrentPageIndex + = 1;
Ee. CurrentPageIndex = this. CurrentPageIndex;
Ee. PageCount = this. PageCount;
Ee. PageSize = this. PageSize;
This. Onpageindexchanged (EE);
}
handled = TRUE;
}
The code first defines a page parameter class object, and then uses the conditional statement to determine whether the current is the last page, if it is not the last page, read the current page information (current page, number of pages, page record) from the main control, and assign the value to the Gridpagechangedeventargs object. This is then called as a parameter of the page parameter object. The Onpageindexchanged method raises the event. Also, note the combination usage of the CommandName and CommandArgument properties of the LinkButton.
Finally, take a look at the Onpageindexchanged method code:
<summary>
For more information on this book, please see:
///Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
</summary>
protected new void onpageindexchanged (Gridpagechangedeventargs e)
{
Gridpagechangedeventhandler handler1 = (Gridpagechangedeventhandler) base. Events[grid.eventpageindexchanged];
if (handler1! = null)
{
Handler1 (this, e);
}
}
The functionality of this method is from base. The events object takes an event reference handle with Grid.eventpageindexchanged as key and raises the event method body if the handle is not NULL (the developer has registered a paging event).
Finally, it is important to note that if you customize the event argument class and require that the type of the change parameter object be automatically displayed in the event body registered by the developer, such as:
protected void Grid1_pageindexchanged (object source, Gridpagechangedeventargs e)
{
... ...&NBSP; the second parameter in
}
is displayed as the Gridpagechangedeventargs type, not the default EventArgs type. We also have to define our own delegate (as in this example defines the delegate Gridpagechangedeventhandler) The default delegate, EventHandler, is the base class System.EventArgs, that is, if the default delegate EventHandler and the Gridpagechangedeventargs class are used here, the following code statement is generated:
protected void Grid1_pageindexchanged (object source, EventArgs e)
{
   //... &NBSP;
}
to see that the parameter becomes the EventArgs type. This makes it impossible to take advantage of the data in the Gridpage Changedeventargs class that we define ourselves. Although you can use (Gridpagechangedeventargs) EventArgs to convert the Gridpagechangedeventargs object can also get the data, but I understand that there is no developer so use, Because no one knows there's a Gridpagechangedeventargs class. This section of the
explains how to customize your own parameter classes and delegates, and illustrates their application in real-world development, using the grid paging feature as an example. The next section begins by explaining the event mechanism of a composite control.

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.