Thoughts Caused by a bug-ASP. NET page loading sequence Discussion

Source: Internet
Author: User

ArticleReprinted from: http://it.dianping.com/asp_net_page_load_order_problem_discussion.htm

Some time ago, I encountered a strange case. This is the case. When I render the master page, I spit some performance data (such as the number of SQL executions) for monitoring, however, I always feel that the data content of each monitoring is incomplete. It is clear that 10 SQL statements are required to load a page, and only 8 or 9 results are monitored. However, one-step debugging finds that all SQL statements are executed. Where can I find any SQL statements without supervision?

First, locate the problematic SQL statement. I found that the unmonitored SQL statement was executed in the render event of the user control. I probably guessed the problem. Maybe the order of page loading caused the render to come out before the metric value is counted? So I went over msdn, according to The msdn records (see: http://msdn.microsoft.com/zh-cn/library/dct97kc3.aspx), the page loading order is like this:

The init event of the master page control.
Content Control init event.
The init event on the master page.
Content Page init event.
Content Page load event.
Master page load event.
Content Control load event.
Content Page prerender event.
The prerender event on the master page.
The prerender event of the master page control.
Content Control prerender event.

The prerender event is listed here, and what I need is the trigger sequence of the render event, so I wroteProgramThe test result is as follows:

Content Page Control init
Master page control init
Master page init
Content Page init
Content Page Load
Master page Load
Content Page Control Load

Master page control load

Content Page prerender
Master page prerender
Content Page Control prerender

Master page control prerender

Content Page: prerender complete

Content Page render
Master page render
Content Page Control render

Master page control render

The problem lies in the page loading order. Because the render event on the master page is before the render event of the Content Page Control, I first output the statistical value and then execute the SQL statement. As a result, the monitoring data is incorrect.

I simply thought about the solution. In order to make the SQL of the Content Page Control be monitored, I moved the SQL Execution function to the load event for execution. This is actually the case from the design point of view. load is responsible for loading data, and render is responsible for displaying data. It is necessary to save two steps and map them step by step, in the end, we will encounter such a problem (--#).

At the same time, I also found that the master page control is the last render. So if you can put the monitored items in a master control, you can handle them to the SQL statement in the render of the Content Page Control. If SQLCodeIf many of the controls that have been written to the content page cannot be migrated to the load at half past one, you can consider migrating monitoring to the render of the master page control.

However, no matter which solution, the most fundamental thing is to understand the real Loading Order of the page. Otherwise, the corresponding solution cannot be provided. However, it is not easy to remember the order of loading without regular rules. It is better to mark this article and wait until it is used to flip it out and check the order, it should be helpful for your future development :)

To download the test code in the article, go to the original article page: http://it.dianping.com/asp_net_page_load_order_problem_discussion.htm

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.