Autoeventwireup Attribute in Microsoft ASP.NET Web Forms

xiaoxiao2021-04-07  314

Introduction

ASP.NET Supports Two Methods To Author Pages:

In-line code code-behind

In-line code is code that is embedded directly within the ASP.NET page. Code-behind refers to code for your ASP.NET page that is contained within a separate class file. This allows a clean separation of your HTML from your presentation logic .

When we use Microsoft Visual Studio .NET to create ASP.NET Web Forms, code-behind pages are the default method. In addition, Visual Studio .NET automatically performs precompilation for us when we build our solution.

A little bit of background

Directives in ASP.NET control the settings and properties of page and user control compilers. They can be included anywhere on a page, although it is standard to place them at the beginning. Directives are used in both .aspx files (ASP.NET pages ) and .scx files. ASP.NET PAGES ACTUALLY Support Eight Different Directives.

@ Page @ Control @ Import @Mplements @ register @ askEMBLY @ OutputCache @ reasonnce

Page directives are the most commonly used directives, and are used to edit a wide variety of settings that control how the page parser and page compiler work. The following is a list of some of the more commonly used page directive attributes in ASP.NET.

@ Page language = "c #" codebehind = "Webform1.aspx.cs"

Autoeventwireup = "false" inherits = "TestWebapp.webform1"

Language indicates the language in which the inline script code within the ASP.NET page is written (the code between <%%> tags). The value of this attribute can be C #, VB, or JS. Codebehind indicates the name of the file being used as the code supporting this ASP.NET page This file should reflect the Language setting;. that is, if the language being used is C #, the CodeBehind file should have a .cs extension and be written in C # Inherits indicates a qualified. . class from which this ASP.NET page should inherit Generally, this will be the name of the class described in the code-behind file AutoEventWireup is a Boolean attribute that indicates whether the ASP.NET pages events are auto-wired.Note.: In the above case, ASP.NET compiles the code-behind page on the fly. We have to note that this compilation step only occurs when the code-behind file is updated. Whether the file has been updated or not, well this is detected THROUGH a TIMESTAMP CHANGE.

TO GET to the Real Thing

The AutoEventWireup attribute may have a value of true or false. When an ASP.NET Web Application is created by using Microsoft Visual Studio .NET, the value of the AutoEventWireup attribute is set as false.

We can Specify The Default Value of The Autoeventwireup Attribute in The Following Locations:

The machine.config file. The web.config file. Individual Web Forms (.aspx files). Web user controls (.ascx files)

The value of the autoeventwireup attribute can be declared in the section in the machine.config file or the weight, as stock:

/>

If you make these changes in the Machine.config file, the changes affect all ASP.NET Web Forms on the computer. If you make these changes in the Web.config file, the changes affect only the application that the file belongs to. However , To make change, we have to add the autoeventwireup attribute to the autoeventwireup attribute to the autoeventwireup attribute, as shown above.check Out the code

When we create a new ASP.NET Web Application in Visual Studio .NET, as mentioned earlier, by default, the value of the AutoEventWireup attribute is set to false in the .aspx page and event handlers are automatically created. We can find this in The InitializationComponent Method:

This.Load = New System.EventHandler (this.page_load);

The Best Way To See The Working of this Attribute Would Be:

Declare a string variable msg as public in Webform1.aspx.cs. In the html section of Webform1.aspx, Enter The Following Code in The section: <% response.write (msg);%> in the point_load, you Could Enter a value for the variable msg declared. msg = "we are in page_load ()";

ON Running The Application, You Will Get The Message We are in page_load () [Hereafter Referred to as message]. Note: this is in the default case where the attribute is set to false.

Now try commenting the event handler code for the Page_Load in the aspx.cs file;. And set the AutoEventWireup attribute to false in the .aspx page On running the application this time, you will not get the message.

Now with the event handler code for the Page_Load in the aspx.cs file still commented;. Set the AutoEventWireup attribute to true in the .aspx page On running the application this time, you will get the message.

Reason: In the case where AutoEventWireup attribute is set to false (by default), event handlers are automatically required for Page_Load or Page_Init However, when we set the value of the AutoEventWireup attribute to true, the ASP.NET runtime does not require events. to specify event handlers like Page_Load or Page_Init.A thing to be kept in mind is that the AutoEventWireup attribute of the Page directive is set to true by default for the machine (check out the value of this attribute in the machine.config) but set to false by default for a .aspx page). So if it is missing, since by default it is true (ie, at the machine level), the page framework calls page events automatically, specifically the Page_Init and Page_Load methods. In that case No expegate is needed.

Performance Issues

We must not set the value of the AutoEventWireup attribute to true if performance is a key consideration. If we set the value of the AutoEventWireup attribute to true, the ASP.NET page framework must make a call to the CreateDelegate method for every Web Form ( .aspx page), and INSTEAD OF RELYING ON The Automatic Hookup, Manually Override The Events from The Page.

Credits

The whole thing was really simple but I just thought of posting it online; hope it helps someone To give credit to where it is due Most of the information was garnered from different places on the net I've just complied them together and... Added A Bit of My OWN TO HELP FOLKS Along Their Way. I Was Also Able To this in msdn online (article ID: 317690) But for vb.net.

转载请注明原文地址:https://www.9cbs.com/read-132423.html

New Post(0)