How does Asp.net dynamically call the processing method of Object events?

Source: Internet
Author: User

 

For example, how can I call the method specified by onpageindexchanging for a gridview? The premise is that I don't know what this method is.

Question added:

Thank you for your answers.
The above is just an example. I Don't Want To pagination, but just want to dynamically call the method specified by the event Delegate. It should be related to reflection, but no relevant method is found. For example, if I have a textbox, I don't know what the ontextchange method is, but I still want to call this method because the parameters of the event Delegate are fixed, so this idea should be feasible, but I don't know the specific method.
========================================================== ================================== // Define the event container. If your inherited class has one, you do not need to define it.
Private EventHandlerList _ events;
Protected EventHandlerList Events
{
Get
{
If (_ events = null)
{
_ Events = new eventhandlerlist ();
}
Return _ events;
}
}
// Define a delegate in the class
Public Delegate void myhandler (Object sender, event E );

// Define the event key
Private Static readonly object myobj = new object ();

// Use this delegate to define the event

Public event myhandler myevent
{
Add
{
Events. addhandler (myobj, value );
}
Remove
{
Events. removehandler (myobj, value );
}
}

// Use the event Method
Private void onmymethodhandler (event E)
{
Myhandler handler = (myhandler) events [myobj];
If (handler! = NULL)
{
Handler (this, e );
}
}

Public void mymethod2 (){

// Call this method where you want to use it
Onmymethodhandler (event. Empty );
}

// This is what you did not know the specified method. B may be used by others.
Public void B (Object sender, event e ){
}
// Others use this method
// If you are a Class
A A = new ();
A. myevent + = new myhandler (B );

// When you run mymethod2, its B will run.

// If you want B to return a value, you need to define the event yourself. You can use e to communicate with B.

========================================================== ======================================
Front-end:
<Asp: gridview id = "gridview1" runat = "server" onpageindexchanging = "AA">
</ASP: gridview>

Background:
Public viod AA (Object sender, gridviewpageeventargs e ){
Gridview1.pageindex = E. newpageindex;
}

Or
Front-end:
<Asp: gridview id = "gridview1" runat = "server">
</ASP: gridview>
Background
Protected void page_load (Object sender, eventargs e ){
Gridview1.pageindexchanging + = new gridviewpageeventhandler (AA );
}
Public viod AA (Object sender, gridviewpageeventargs e ){
Gridview1.pageindex = E. newpageindex;
}

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.