CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

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



Comments

Paul Wilson said:

This is not a VB thing -- it works the same way in C#!
# February 12, 2004 9:21 AM

Peter van Ooijen said:

Paul, to my amazement, you are abolutely right. What a shame.. The post is updated..
# February 12, 2004 9:31 AM

Voltaire said:

Please explain moew in detail
# March 18, 2004 9:54 PM

Peter van Ooijen said:

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.
# March 20, 2004 2:10 AM

Justin Moore said:

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
# May 27, 2004 12:56 AM

Peter van Ooijen said:

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.
# May 29, 2004 1:54 PM

Ather Hussain Murtuzapurwala said:

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.
# June 9, 2004 8:45 AM

Peter van Ooijen said:

AutoEventWireUp is crazy ! They should ban it :> I do
# June 9, 2004 2:59 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

This Blog

Syndication

News