EvilDr EvilDr - 1 month ago 9
Vb.net Question

VB to C# - auto naming of event handlers vs explicit wiring

Sorry if this is a duplicate; I have searched extensively for a basic explanation.

TL;DR - I already know how to wire up events, my question relates to supporting information and event handler naming convention(s), which I cannot find an answer to. Having already read here, here, here, here, and here I think information overload has confused me, or I'm overlooking the obvious.

I have a huge web forms (VB) solution that needs converting to C#, starting with some Web Forms Server Control projects. Within each, VB uses the

Handles
word to wire up event handlers:

Public Class Accordion
Inherits CompositeControl

Private Sub Accordion_Init(sender As Object, e As System.EventArgs) Handles Me.Init

End Sub

Private Sub AccordionPanelHolder_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Private Sub AccordionPanelHolder_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

End Sub

End Class


When using any kind of code converter, and from my research, the C# approach means manually wiring the event handlers (code shortened for brevity):

public Accordion()
{
PreRender += AccordionPanelHolder_PreRender;
Load += AccordionPanelHolder_Load;
Init += Accordion_Init;
}
private void AccordionPanelHolder_PreRender(object sender, System.EventArgs e)
{
// ...
}


I'm happy with the above, however, if I create a new web form in VS then the default code is simply this:

public partial class TestPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// to do
}
}


There is no event logic handler logic in the page. I see that
AutoEventWireUp="True"
, when usually I would have it as
False
. My questions therefore:


  1. What/Where is the naming convention defined that C# uses to auto wire up event handlers, as per the last example? I'm nervous that methods could run twice. I see that VS doesn't have the drop-down functionality for C# to create empty event handlers are that wired up.

  2. If
    AutoEventWireUp="False"
    , do I have to manually wire up every single event (I expect so, which is a good thing [I think...])?


Answer

When AutoEventWireUp is true, the following methods would be looked for:

  • Page_PreInit
  • Page_Init
  • Page_InitComplete
  • Page_PreLoad
  • Page_Load
  • Page_LoadComplete
  • Page_DataBind
  • Page_SaveStateComplete
  • Page_PreRender
  • Page_PreRenderComplete
  • Page_Unload
  • Page_Error
  • Page_AbortTransaction
  • Page_CommitTransaction

So, the methods must use this exact name.

If you set AutoEventWireUp to false, you'll have to add the handler for any of this events by hand.

You can have a look here for more info.