What is AutoEventWireup in asp.net - Part 133

  • دعنا نشارك هذا :
ترتيب الدرس : 133
المدة : 00:07:27
التحكم :
Text version of the video
http://csharp-video-tutorials.blogspot.com/2013/03/what-is-autoeventwireup-in-aspnet-part.html

Slides
http://csharp-video-tutorials.blogspot.com/2013/08/part-133-auto-event-wireup-in-aspnet.html

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation1

All ASP .NET Text Articles
http://csharp-video-tutorials.blogspot.com/p/free-aspnet-video-tutorial.html

All ASP .NET Slides
http://csharp-video-tutorials.blogspot.com/p/aspnet-slides.html

All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view1&sortdd

All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists

Let us understand the use of AutoEventWireup property with an example. Create an asp.net web application project.

A webform in asp.net raises several events in it's life cycle. The following are few of those events.
1. Page Load
2. Page Load Complete
3. Page PreRender
4. Page PreRenderComplete

Copy and paste the following code in WebForm1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Page Load");
}

protected void Page_LoadComplete(object sender, EventArgs e)
{
Response.Write("Page LoadComplete");
}

protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("Page PreRender");
}

protected void Page_PreRenderComplete(object sender, EventArgs e)
{
Response.Write("Page PreRenderComplete");
}

Now in WebForm1.aspx, set AutoEventWireuptrue
Run the application, and notice that the above event handler methods are executed as expected. We did not explicitly associate event handler methods to the events, but still the event handler methods are hooked up to their respective events. This is because we have AutoEventWireup attribute set to true.

Now, set AutoEventWireupfalse
Run the application, and notice that none of the above event handler methods are executed. This is because, when "AutoEventWireup" property is set to false, the event handler methods does not get automatically associated with their respective events. We have to explicitly associate them by overriding OnInit() method as shown below. Now, copy and paste the following code in webform1.aspx.cs
protected override void OnInit(EventArgs e)
{
this.Load + new EventHandler(Page_Load);
this.LoadComplete + new EventHandler(Page_LoadComplete);
this.PreRender + new EventHandler(Page_PreRender);
this.PreRenderComplete + new EventHandler(Page_PreRenderComplete);
}

Run the application, and notice that the above event handler methods are executed as expected.

Now, set AutoEventWireuptrue.

Run the application, and notice that every event handler method is executed twice. This is because,
1. Setting AutoEventWireuptrue, registered the event handler method's once, and
2. Overriding OnInit() method, has registered the same event handler method again

Important points to remember:
1. When AutoEventWireup is set to true and if you want the event handlers to be wired up with their events automatically, the event handler names should follow the standarad naming convention - Page_EventName.
2. AutoEventWireup can be set in the page directive or in web.config file.
3. To set autoEventWireup in web.config, use pages element.
4. If autoEventWireup, is set at both webform and web.config level, webform setting will take precedence over web.config setting.

So, AutoEventWireup is a boolean property which, when set to true, the page event handler methods are automatically wired with their respective events. If this property is set to false, then the event handler methods need to be explicitly associated with their respective events.

سيتم تفعيل التعليقات قريبا

y
Sep 23, 2020 07:32:26
youtube