質問

When I double click on a button (myButton) in Design view of a .aspx web form, an event handler is automatically generated in the code behind: protected void myButton_Click(object sender, EventArgs e)

Now if I understand correctly, in order to associate that method with the Button.Click event, somewhere there has to be something like: myButton.Click += new EventHandler(this.myButton_Click);

However, I can't seem to find that anywhere. I've used Ctrl+F for the entire solution and I've checked the mywebform.aspx.designer.cs.

At first I thought it was because the .aspx page's AutoEventWireup was set to true. However, even after making AutoEventWireup false, the button still responds to being clicked by running the code in protected void myButton_Click(object sender, EventArgs e)

I understand that you shouldn't mess with generated code, and I don't intend to, I just want to know more about how this is working under the hood.

役に立ちましたか?

解決

The assignment of the event handler is actually done in the asp markup. Here's a link to a bunch of different properties that can be declaratively assigned to your button.

Here's another MSDN link about using the OnClick attribute.

I'm guessing that your ASP markup for the button has the following property assigned:

OnClick="myButton_Click"

As for how it gets translated into an assignment, the page gets compiled at runtime upon the first time it is requested (ASP.NET Compilation Overview).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top