stevee1986 stevee1986 - 2 months ago 24
ASP.NET (C#) Question

When does .Net assign event handler methods to control events?

I am trying to understand (due to mere curiosity) when an event handler method gets assigned to to the event in .NET

In this example, let's say we have an ASP webform, WebForm1 with a button, Button1. By default, when we add a button to the page, .NET / VS lets us define a method that is run when the button is clicked, within the WebForm1 class. I am interested to know what it is doing this behind the scenes.

I know this is to do with events and delegates and my understanding of what is happening here is as follows:

When we create a new webform in an ASP project, .NET declares it as a new class that inherits from the page class;

public partial class WebForm1 : System.Web.UI.Page
{
}


I can add a button to the page and .NET creates an instance of the button object (albeit, in a designer file)

public partial class WebForm1 : System.Web.UI.Page
{
protected Button Button1;
}


I can declare a method that runs when the button is clicked

public partial class WebForm1 : System.Web.UI.Page
{
protected Button Button1;
protected void Button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Button clicked");
}
}


So what has happened behind the scenes for me to get this functionality out of the box?

The System namespace defines an EventHandler which is a delegate;

public delegate void EventHandler(object sender, EventArgs e);


The button class has an instance of this delegate

public event EventHandler Click;


and somewhere the Button1_Click method is assigned to Click event in Button1

This would be something along the lines of

Button1.Click += Button1_Click;


my question is, where is this happening?

Is this something that is happened in the WebForm1 class?

EDIT

As others have said, there is some markup that I forgot to mention as follows:

<asp:Button ID="Button1" runat="server" Text="Button" />


Also, as mentioned by mybirthname and This Resource, which I have since found, if I decide to change

AutoEventWireup="true"


to

AutoEventWireup="false"


there are a number of places I can do the assignment myself.

so where will .NET / VS be doing this is AutoEventWireUp is true, it doesn't seem to have generated any code anywhere in the project?

Answer Source

You could do it in Page_Load event.

protected void Page_Load(object sender, EventArgs e)
{   

    Button1.Click += Button1_Click;
}

But current code which you have will not work. Currently you have just one button which is not added in the Controls of the page.

You need to have in the aspx as:

<asp:Button runat="server" ID="Button1"  />

Additionally you can here the OnClick event

<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" />

If you want to have Button Control added in the code behind the proper place is to be added in the CreateChildControls() method

public partial class WebForm1 : System.Web.UI.Page
{
    protected Button button1=null;
    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        button1 = new Button();
        button1.ID="Button1";
        button1.OnClick += Button1_Click;
        this.Controls.Add(button1);   
    }

}

This will add the button to the page if you want to have it as control in the code behind.But this is really not good way to do it when you have aspx and you can write the proper position of the button. This will render the button under all other controls. This approach is used when you create custom controls.