Asp. NET to implement custom own delegate and event parameter class _ Practical Tips

Source: Internet
Author: User

The example of this article is about the method of customizing the ASP.net and event parameter classes in the ASP.net, which has a good reference value for learning. The specific methods are as follows:

Generally in actual development, when the event does not need to pass data information, like the Kingtextbox control event above, the parameter passed when the event is raised is Eventargs.empty, as follows:

OnTextChanged (Eventargs.empty);

This is because the TextChanged event of the control Kingtextbox is simpler, and there is no need for parameter objects to pass data. But like some complex controls such as the GridView button command event, you must have a parameter command that indicates which button was clicked, and the paging event requires the EventArgs parameter object to pass the current page information to the second parameter of the event body of the page background code. The developer then obtains the corresponding page data from the database based on this page parameter, as well as the ItemDataBound event, and passes the current row information and index data as EventArgs parameters to the event instance.

When these situations are encountered, we need to define our own event parameter classes and delegates instead of using the default System.EventArgs class. Below is a description of the method for defining the event parameter classes and delegates, using the page-turning function of the grid control, 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 information about paging, which is derived from the System.EventArgs class, where it is not mandatory to inherit from the class, nor is it possible to inherit it, but it is advantageous to inherit from the class. First look at the code for the System.EventArgs base class:

<summary>
/// 
///</summary> public
class EventArgs
{
  //= Event public with no event data
  static readonly EventArgs Empty;
  Initializes a new instance of the System.EventArgs class public
  EventArgs ();
}

In addition to a construction method in EventArgs, there is also a Empty attribute of its own type, from which you can know that the preceding is invoked through OnTextChanged (Eventargs.empty); The use of Eventargs.empty as an empty parameter to pass in the event-raising method. The point here is that if our Gridpagechangedeventargs class is inherited from EventArgs, not only can the Gridpagechangedeventargs class object be passed, You can also pass an empty parameter object in the form of Gridpagechangedeventargs.empty.

Some properties are defined in the Gridpagechangedeventargs method body, representing the current page (CurrentPageIndex), the total number of pages (PageCount), and the page size (PageSize) respectively. These are our customized 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 such an event method: The first argument is type Object, and the second argument is the page parameter class objects 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 the same as their own two parameter types. The following is the event background code that is registered in 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 argument type, the Griddocpagechangedeventargs type we define, can be passed directly through E in the event method body. CurrentPageIndex and E.pagesize Get the data, this application is much more.
Now that the page parameter objects and delegates are well defined, let's say 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 ("Paging event")]
Public event Gridpagechangedeventhandler pageindexchanged
{
  add
  {
    base. Events.addhandler (grid.eventpageindexchanged, value);
  }
  Remove
  {
    base. Events.removehandler (grid.eventpageindexchanged, value);
  }
}

Here we continue to use the efficient Event collection list object base. Events, the name of the event is pageindexchanged, and the delegate type is the delegate type Gridpagechangedeventhandler that we defined previously.
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, which handle child control events in bubbling form, and then explain the method in detail when the composite control bubbles the event mechanism. In addition, in the control of the page in advance placed four buttons to turn the page, respectively, "first page", "prev", "Next", "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 commands and parameters of the button. Here 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 determines whether the current is the last page through a conditional statement, and if not the last page, reads the current page information (current page, number of pages, page records) from the main control, and assigns the value to the Gridpagechangedeventargs object. This is then called with the page parameter object as an argument. The Onpageindexchanged method raises an event. Also, note the combined use of the CommandName and CommandArgument properties of LinkButton.
Finally, take a look at the Onpageindexchanged method code:

<summary>
///</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 out the event reference handle with Grid.eventpageindexchanged as key, and raises the event method body if the handle is not NULL (the developer registers the paging event).
Finally, it is important to note that if you customize the event parameter class and require that the type of the parameter object be automatically displayed in the event body that the developer registers, such as:

protected void Grid1_pageindexchanged (object source, Gridpagechangedeventargs e)
{
  ///... 
}

The second parameter is shown as the Gridpagechangedeventargs type, not the default EventArgs type, and we must also define our own delegate (as this example defines the delegate Gridpagechangedeventhandler) , and the default delegate eventhandler the corresponding parameter type 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)
{
  ///... 
}

You can see that the parameter changes to EventArgs type. This makes it impossible to take advantage of the data in our own defined Gridpage Changedeventargs class. Although you can use the (Gridpagechangedeventargs) EventArgs to convert the data in the Gridpagechangedeventargs object, it is not known that developers are using it. Because no one knows there's a Gridpagechangedeventargs class.

This article mainly explains how to customize their own parameter classes and delegates, and the grid's paging function as an example to demonstrate its application in the actual development.

Interested readers can refer to the debugging run this article example, I believe there will be a new harvest.

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.