Peter's Gekko

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
AutoEventWireUp, the Gordian knot

In VB.NET ASP.NET you have something called AutoEventWireUp. It is set in the directives of the webform's HTML

<%@ Page Language="vb" AutoEventWireup="true" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1"%>

When set to true asp.net will automatacally "wire up" methods of the page's class with events in the lifecycle of the page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ListBox1.Items.Add("From Load")
End Sub

An alternative is to explicitly states in you method declaration that it will handel a certain event

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ListBox1.Items.Add("From Load")
End Sub

Now guess what happens when both AutoEventWireUp is set to true and Handles is specified ? Exactly, the method will execute twice.

Anders Hejlsberg, the creator of my beloved C#, always makes a strong point of not adding a new feature to the C# language when it will open the possibility to do the same thing on two different ways. Because that could be confusing. This VB wireup feauture is more than confusing, to me it's just a mess. Two very different ways to get the same thing done, one in a directive and one in "normal"code. And both ways hide the fact that you are not assigning one specific method as a handler for an event. In .net multiple methods can subscribe to an event, the C# version of the generated InitializeComponenet demonstrates this. The same method can even subscribe twice to the same event. Like just demonstrated.

Peter


Posted 02-12-2004 1:44 PM by pvanooijen

[Advertisement]

Comments

Romualdas wrote re: AutoEventWireUp, the Gordian knot
on 02-12-2004 9:06 AM
There was a question in the exam about it: &quot;The method was called twice, what is the likely reason?&quot; I never use it ...
Paul Wilson wrote re: AutoEventWireUp, the Gordian knot
on 02-12-2004 9:21 AM
This is not a VB thing -- it works the same way in C#!
Peter van Ooijen wrote re: AutoEventWireUp, the Gordian knot
on 02-12-2004 9:31 AM
Paul, to my amazement, you are abolutely right. What a shame.. The post is updated..
Voltaire wrote re: AutoEventWireUp, the Gordian knot
on 03-18-2004 9:54 PM
Please explain moew in detail
Peter van Ooijen wrote re: AutoEventWireUp, the Gordian knot
on 03-20-2004 2:10 AM
There are two ways to hook up an event : from code and from markup. C# default goes for the first option, VB for the second. My point is that two is too much. As an OOP coder I go for the code way.
Justin Moore wrote re: AutoEventWireUp, the Gordian knot
on 05-27-2004 12:56 AM
Just to further support your point....
MS states that you should not intentionally enable AutoEventWireUp for C# code, because it requires that the page event handlers have specific, predictable names. If you aren't exact on these names, events can be fired twice. Studio will automatically call events based on their names. This can result in the same event code being called twice when the page runs. What this means for the csharp developer - stick to Codebehind.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconWebFormsEventModel.asp
Peter van Ooijen wrote re: AutoEventWireUp, the Gordian knot
on 05-29-2004 1:54 PM
VB.NET and C# behave the same, it's VS applying a different default to the page. At first sight it looks like a language matter, but it is actually a VS settings matter.
Ather Hussain Murtuzapurwala wrote re: AutoEventWireUp, the Gordian knot
on 06-09-2004 8:45 AM
Yeh,Ites happen because

If you 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). Instead of relying on the automatic hookup, manually override the events from the page.

This statement is from msdn.microsoft.com
actually I forgot the exact link.
Peter van Ooijen wrote re: AutoEventWireUp, the Gordian knot
on 06-09-2004 2:59 PM
AutoEventWireUp is crazy ! They should ban it :> I do

Add a Comment

(required)  
(optional)
(required)  
Remember Me?