Use simple code to test whether you really understand the implementation of events and delegation in ASP. NET.

Source: Internet
Author: User

Page:

<% @ Page Language = " C # " Codebehind = " Default. aspx. CS " Autoeventwireup = " True " Inherits = " Webapplication1. _ default "   %>

Background:

{
Cnblogs_code_show ('8e4829db-54ca-4ed6-b745-066a89ee342f ')
} "> {
Cnblogs_code_hide ('8e4829db-54ca-4ed6-b745-066a89ee342f ', event)
} "Src =" http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif "alt =" "> Code

Protected   Void Page_load ( Object Sender, eventargs E)
{
Response. Write ( " Page is loaded by page_load! <Br> " );
}

Protected Override VoidOnload (eventargs E)
{
Response. Write ("Page is loaded by onload! <Br>");
Base. Onload (E );
}

Problem:

1. What is the output if autoeventwireup = "false?

2. If you comment out base. onload (E); what is the output?

3. What are the results of other combinations of autoeventwireup settings and base. onload (e) Comments?

 

I wrote an article before.ArticleUnderstanding delegation and events from another perspectiveThis article describes how ASP. NET uses delegation and event mechanisms.

 

Here we will first introduce a shortcut key in vs2008: F12; then we recommend a tool: recycltor. In many cases, the use of tools is more useful than mastering a certain knowledge point. The so-called "give a person a fish, it is better to give a person a fish". F12 can be converted to the definition of the method, and recycltor can "decompile". netProgramSet is a powerful tool for us to learn. net.

 

First, if autoeventwireup = "false", we will find that the page_load method is not executed at all (you can set the breakpoint view ). If you are learning ASP. net2.0 directly, it is very likely that you will ignore this knowledge point (unfortunately, I also learned 2.0 directly ). Autoeventwireup is a newly added attribute of ASP. net2.0 (you can check msdn for details). It is used to automatically bind ASP. net2.o page events!

Let's go back and think about delegation and events! We often say that page events, page lifecycles ......, How are these things actually implemented? Can we see the code they implement?

As you can imagine, if the page_load method needs to be executed, it must be associated with page events. In the page class, an event (such as onload) will be exposed. Then, we also need to "subscribe" the event. I prefer to say that since the page_load method is associated with the event, let the event know which method to call. But how can we verify it? What about clues?

 

Use F12, first.

Public   Partial   Class _ Default: system. Web. UI. Page

Place the cursor over the page class and press F12 and vs2008 to automatically jumpSystem. Web. UI. PageOn the code page, we can find the following code:

{
Cnblogs_code_show ('8bdd4bf0-5919-449b-90a2-74a8a0fd8ae1 ')
} "> {
Cnblogs_code_hide ('8bdd4bf0-5919-449b-90a2-74a8a0fd8ae1 ', event)
} "Src =" http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif "alt =" "> Code

// Summary:
// Occurs when page Initialization is complete.
[Editorbrowsable (editorbrowsablestate. Advanced)]
Public   Event Eventhandler initcomplete;
//
// Summary:
// Occurs at the end of the load stage of the page's life cycle.
[Editorbrowsable (editorbrowsablestate. Advanced)]
Public   Event Eventhandler loadcomplete;
//
// Summary:
// Occurs at the beginning of page initialization.
Public   Event Eventhandler preinit;
//
// Summary:
// Occurs before the page system. Web. UI. Control. Load event.
[Editorbrowsable (editorbrowsablestate. Advanced)]
Public   Event Eventhandler preload;
//
// Summary:
// Occurs before the page content is rendered.
[Editorbrowsable (editorbrowsablestate. Advanced)]
Public   Event Eventhandler prerendercomplete;
//
// Summary:
// Occurs after the page has completed saving all view State and control state
// Information for the page and controls on the page.
[Editorbrowsable (editorbrowsablestate. Advanced)]
Public   Event Eventhandler savestatecomplete;

Wow, it's all about events! But unfortunately, we failed to find the desired onload event ......

 

Fortunately, we still have this line of code:

Base . Onload (E );

Move the cursorOnload (E)On, then F12, haha, found:

{
Cnblogs_code_show ('7305a847-2525-403c-8e62-1d4b2846b871 ')
} "> {
Cnblogs_code_hide ('7305a847-2525-403c-8e62-1d4b2846b871 ', event)
} "Src =" http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif "alt =" "> Code

//
// Summary:
// Raises the system. Web. UI. Control. Load event.
//
// Parameters:
// E:
// The system. eventargs object that contains the event data.
Protected   Internal   Virtual   Void Onload (eventargs E );

It's not easy. Let's take a closer look. In the control class, page originally inherits the control class, which is somewhat different from our general idea! (I will not expand it here) more comments. We can see that this method is the raise (send) Load event method! In this control class, we can also see the declaration of the load event:

{
Cnblogs_code_show ('13e08d7e-5de0-4900-bf5e-d9512860887c ')
} "> {
Cnblogs_code_hide ('13e08d7e-5de0-4900-bf5e-d9512860887c', event)
} "Src =" http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif "alt =" "> Code

//
// Summary:
// Occurs when the server control is loaded into the system. Web. UI. Page Object.
[Websysdescription ( " Control_onload " )]
Public   Event Eventhandler load;

We can also see the event-bound delegate eventhandler, and then F12, we can see the eventhandler statement:

{
Cnblogs_code_show ('ffd1c5af-01bb-49d7-9a27-dbab5941b411 ')
} "> {
Cnblogs_code_hide ('ffd1c5af-01bb-49d7-9a27-dbab5941b411 ', event)
} "Src =" http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif "alt =" "> Code

// Summary:
// Represents the method that will handle an event that has no event data.
//
// Parameters:
// Sender:
// The source of the event.
//
// E:
// An system. eventargs that contains no event data.
[Serializable]
[Comvisible ( True )]
Public   Delegate   Void Eventhandler ( Object Sender, eventargs E );

It feels so cool to see the code! Right?

 

What's more, we are still using reconfigurator,

{
Cnblogs_code_show ('00eb7a08-c754-425d-8ad7-f9a5b064dd26 ')
} "> {
Cnblogs_code_hide ('00eb7a08-c754-425d-8ad7-f9a5b064dd26 ', event)
} "Src =" http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif "alt =" ">Code

Protected   Internal   Virtual   Void Onload (eventargs E)
{
If ( This . Hasevents ())
{
Eventhandler Handler =   This . _ Occasionalfields. events [eventload] As Eventhandler;
If (Handler ! =   Null )
{
Handler ( This , E );
}
}
}

It is a little different from our imagination, but it is completely understandable, and you can click it further.

 

Now, we can sort out the ASP. NET delegate event mechanism:
1 .. the self-built eventhandler of net determines the method signature of the delegate call (parameters: sender and E, return value void), which is why the event methods we see look exactly the same;

2. In the page class and its parent class, it does define a series of methods for events and raise events. We can use these events (such as writing the page_load () method), or directly override to call these event methods (such as onload () method ). The difference or relationship between the two has always been dizzy.

3. Because of autoeventwireup, we didn't see the "event subscription" code, so we are sorry, so we will implement it ourselves: (first autoeventwireup = "false ")

Protected   Override   Void Oninit (eventargs E)
{

Base . Oninit (E );
This . Load + =   New Eventhandler (page_load );
}

 

 

Now, can you answer the questions at the beginning of this article?

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.