Page_Load () event loading in Asp. NEt twice

Source: Internet
Author: User

When using VS2005 for a WebForm project, we found that many aspx pages are loaded twice when page_load () is loaded, and two identical results are output on the Page, it turns out that the AutoEventWireup attribute is a ghost:

<% @ Page language = "c #" Codebehind = "Result. aspx. cs" AutoEventWireup = "true" Inherits = "test. Result" %>

Solution:

<% @ Page language = "c #" Codebehind = "Result. aspx. cs" AutoEventWireup = "false" Inherits = "test. Result" %>

Cause analysis:

If the AutoEventWireup attribute of the Page command is set to true (or if this attribute is missing because it is set to true by default), the Page framework automatically calls the Page events, namely the Page_Init and Page_Load methods. In this case, no explicit Handles clause or delegation is required.
The disadvantage of the AutoEventWireup attribute is that it requires the page event handler to have a specific and predictable name. This limits your flexibility when naming event handlers. Therefore, in Visual Studio, the AutoEventWireup attribute is set to false by default. The designer generates explicit code used to bind page events to methods.
If AutoEventWireup is set to true, Visual Studio generates the code used to bind the event, and the page framework automatically calls the event based on the event name. This may cause the same event code to be called twice when the page is running. Therefore, when operating in Visual Studio, set AutoEventWireup to false whenever possible.

For more information about AutoEventWireup, see Microsoft MSDN

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.