0

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...])?
Community
  • 1
  • 1
EvilDr
  • 8,943
  • 14
  • 73
  • 133
  • The naming convention is the name as in VB i.e. `ObjectName_EventName` but the handler is done in the designer instead of the code behind – A Friend Nov 03 '16 at 10:42
  • *"In the designer"* - do you mean the HTML window? How would you access events like PreRender in this fashion? – EvilDr Nov 03 '16 at 10:46

2 Answers2

3

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.

Pikoh
  • 7,582
  • 28
  • 53
  • Extremely helpful page link which answers all my queries perfectly. Thank you so much. I would have saved a day's reading if I'd have asked here first... :-/ – EvilDr Nov 03 '16 at 11:07
  • You're welcome. Yeah, you sometimes get answers in StackOverflow ;) – Pikoh Nov 03 '16 at 11:09
1

Not entirely sure if this is a correct answer, but you should think of it as wire up the event from when it is needed. If this is from the creation of your class you can add the hooks in the constructor, or as in asp.net the Page_Load delegate.

Something like this

void Page_Load(object sender, EventArgs e)
{
    PreRender += AccordionPanelHolder_PreRender;
    Load += AccordionPanelHolder_Load;
    Init += Accordion_Init;
}

But it was a long time ago I was working with Web Forms, so maybe there's something I'm forgetting, but this is similar to how I would do it in a regular C# application.

einord
  • 2,278
  • 2
  • 20
  • 26
  • 1
    Thanks. Just one minor point to paraphrase http://stackoverflow.com/a/10200828/792888 - *"... always bind events in Init."* – EvilDr Nov 03 '16 at 11:19