Custom delegate and event parameter classes in ASP. NET

Source: Internet
Author: User

Generally, in actual development, when an event does not need to pass data information, the event like the event of the KingTextBox control above is passed as the parameter EventArgs. Empty when an event is triggered, as shown below:
OnTextChanged (EventArgs. Empty );
This is because the TextChanged event of the control KingTextBox is relatively simple, and no parameter object is required to pass data here. However, for some complex controls such as the button command event of the GridView, a parameter command must be provided to indicate which button is clicked. Page turning event, the EventArgs parameter object is required to pass the current page information to the second parameter of the event body in the background code of the page. Then, the developer obtains data on the corresponding page from the database based on the parameters on this page; for ItemDataBound events, you also need to pass the current Row information and indexes as EventArgs parameters to the event instance.
In these cases, we need to define our own event parameter classes and delegation, instead of using the default System. EventArgs class. The following uses the page flip function of the Grid Control to describe how to define event parameter classes and delegate methods. See the following code:
/// <Summary>
/// For more information about this book, see:
// Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </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 code above defines a parameter class that stores information related to page turning. This parameter class is derived from System. eventArgs class. In this case, the system does not require the class to be inherited. It is also possible not to inherit it, but it is advantageous to inherit the class. Let's take a look at the code of the System. EventArgs base class:
/// <Summary>
/// For more information about this book, see:
// Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </Summary>
Public class EventArgs
{
// Events with no event data
Public static readonly EventArgs Empty;
// Initialize a new instance of the System. EventArgs class
Public EventArgs ();
}
In addition to a constructor, EventArgs also has an Empty attribute of its own type. Here we can know that the OnTextChanged (EventArgs. empty); format, put EventArgs. empty is used as an Empty parameter to pass in the event triggering method. It should be noted that if our GridPageChangedEventArgs class is inherited from EventArgs, it can not only pass the GridPageChangedEventArgs class object, but also use GridPageChangedEventArgs. Empty to pass an Empty parameter object.
Some attributes are defined in the GridPageChangedEventArgs method body, indicating the current page (CurrentPageIndex), total page (PageCount), and page size (PageSize ). These are all custom page information data.
This sentence in the Code:
Public delegate void GridPageChangedEventHandler (object source, GridPage ChangedEventArgs e );
Defines a delegate GridPageChangedEventHandler. This delegate can specify such an event method: the first parameter is of the object type, and the second parameter is the page parameter class object GridPageChanged EventArgs defined above. When registering an event, the delegate ensures that the two parameter types of the event body automatically generated in the code behind the page are consistent with those of the two parameter types of the user. The following is the event background code registered on the page:
Protected void grid#pageindexchanged (object source, GridPageChangedEventArgs e)
{
Int intCurrentPageIndex = e. CurrentPageIndex;
Int intPageSize = e. PageSize;
// Obtain data Logic
}
We can see that the second parameter type is the GridDocPageChangedEventArgs type we have defined. In the event Method body, you can directly obtain data through e. CurrentPageIndex and e. PageSize. This application is much larger.
Now the page parameter objects and delegation have been defined. The following describes how to apply them inside the main control. The event declaration code is as follows:
/// <Summary>
/// For more information about this book, see:
// Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </Summary>
Private new static readonly object EventPageIndexChanged = new object ();
[Category ("Action"), Description ("Page flip event")]
Public event GridPageChangedEventHandler PageIndexChanged
{
Add
{
Base. Events. AddHandler (Grid. EventPageIndexChanged, value );
}
Remove
{
Base. Events. RemoveHandler (Grid. EventPageIndexChanged, value );
}
}
Here we will continue to use the base. Events object in the high-efficiency event set described in section 5.3.1. The event name is PageIndexChanged, and the delegate type is the previously defined delegate type GridPageChangedEventHandler.
The core code for triggering an event is as follows:
/// <Summary>
/// For more information about this book, see:
// Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </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 method is mainly used in composite controls to process subcontrol events in the form of bubbles. This method is described in detail later when the composite controls bubble process the event mechanism. In addition, four page flip buttons are pre-placed in the page flip bar of the control, indicating "Homepage", "Previous Page", "Next page", and "last page", respectively ", set the CommandName attribute to "Page", CommandArgument to "ButtonFirst", "ButtonPrev", "ButtonNext", and "ButtonLast ".
In this way, you can determine the logic to be executed based on the button commands and parameters. Here we only use the "next Page" (Command = "Page" & CommandArgument = "ButtonNext") as an example to explain the code logic:
/// <Summary>
/// For more information about this book, see:
// Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </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 a Condition Statement to determine whether the current page is the last page. If it is not the last page, read the information of the current page (current page, number of pages, number of page records) from the master control, assign a value to the GridPageChangedEventArgs object, and call this with the page parameter object as the parameter. the OnPageIndexChanged method raises an event. In addition, pay attention to the combined usage of the CommandName and CommandArgument attributes of LinkButton.
Finally, let's take a look at the OnPageIndexChanged method code:
/// <Summary>
/// For more information about this book, 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 function of this method is from base. the Events object is retrieved from the Grid. event reference handle where EventPageIndexChanged is the Key. If the handle is not null (a developer registers a paging event), the event Method body is thrown.
Finally, it is very important to note that if the event parameter class is customized and the type of the parameter object must be automatically displayed in the event body registered by the developer, for example:
Protected void grid#pageindexchanged (object source, GridPageChangedEventArgs e)
{
//... ...
}
The second parameter is displayed as the GridPageChangedEventArgs type, instead of the default EventArgs type. We must also define our own delegate (for example, we have defined the delegate GridPageChangedEventHandler ); the parameter types corresponding to the default delegate EventHandler are base class System. eventArgs: if the default EventHandler and GridPageChangedEventArgs classes are used together, the following code statement is generated:
Protected void Grid1_PageIndexChanged (object source, EventArgs e)
{
//... ...
}
It can be seen that the parameter has changed to EventArgs type. In this way, we cannot use the data in the GridPage ChangedEventArgs class defined by ourselves. Although you can use the (GridPageChangedEventArgs) EventArgs method to convert it, you can also get the data in the GridPageChangedEventArgs object, but I know no developers have used it because no one knows there is a GridPageChangedEventArgs.
This section describes how to customize your own parameter classes and delegation, and uses the Grid paging function as an example to demonstrate its application in actual development. The next section describes the event mechanism of the composite control.

 
Author Zheng wenliang

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.