Understanding AutoEventWireup attribute in Asp.net page. Event handlers.

The AutoEventWireup attribute indicates if the page events are auto-wired up. If the attribute is set to true .Net framework searches automatically for methods which names are created in the way Page_xxxxx (Page_Load, Page_Init etc.) and connects them to the events. Otherwise the developer or the environment must do that using delegates:

this.Load += new System.EventHandler(this.Page_Load);

for all handled page events. Setting AutoEventWireup to false is the better way because of the performance -> the framework uses only the methods with code!

The attribute can be set on several places: in machine.config, web.config and for every page.

Note:
The note is not connected directly to the page events, but to events of postback controls like button, linkbutton etc.
In VB.NET the event handlers can be attached to the events using the keyword "Handles".

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click

This method will handle the event Click and will work like adding the attribute
OnClick="LinkButton1_Click" to the LinkButton tag. But NEVER USE both of the ways at the same because this will invoke the method twice!!
Consider using keyword "Handles" OR OnEvent="", but never use them together.

No comments:

Post a Comment