views:

38

answers:

3

Say you have a Button in your Web form, with OnClick bound to a code-behind event.

You then move that button up into the master page, by adding it to the Controls collection of a control in the master page. This is done at run-time, in the Page_Load event.

Where is the OnClick event bound to now? Still the code-behind of the Web form, or is it looking in the code-behind of the master page?

I did just this, and now my button doesn't do anything. It posts the page back, but doesn't actually run the bound event in the code-behind.

I checked the HTML of the button in both cases. The only thing that changed was the ID and the name, to reflect the change in naming container:

In the Web form ("MainContent" is a ContentPlaceholder):

<input type="submit" id="ctl00_MainContent_DeleteButton" value="Yes, Delete" name="ctl00$MainContent$DeleteButton"/>    

In the master page:

<input type="submit" id="ctl00_DeleteButton" value="Yes, Delete" name="ctl00$DeleteButton"/>

I have run the debugger and can confirm that it's not touching the bound event any longer.

Is it the ID or the name that binds it to an event? If so, will my moving it into the master page break this binding?

A: 

What binds a control to a codebehind event is an attribute (ex: <asp:Button ... OnClick="btnSubmit_click">) and this attribute seems to be missing in your examples above.

Double click on the button on your master page and it should wire up the event. If you get serious with the event handling you'll find plenty of interesting stuff too :)

C Bauer
You misunderstood. The HTML from above was the *rendered* HTML on the page. And I can't click on the button in my master page because it gets moved *to* the master page in the Page_Load event. It starts in the Web form, where the binding is fine.
Deane
Oh. Is there a chance you can, in the procedure that generates the button on the master, wire up a similar event from codebehind? IE: Button.onclick = originalButtonmethod_click()
C Bauer
A: 

If the control is in the master page, the engine will look for the event handler in the master page's code behind. Event handlers need to either be

a. declared in the same scope in which they are defined, or

b. programmatically assigned.

David Lively
The controls *starts* in the Web form. It gets moved to the master page during Page_Load. So, what scope is it in?
Deane
You weren't clear about when and how the button was moved. You've also not mentioned how the event handler is assigned.
David Lively
I'm sorry -- I edited my post above to indicated that it's moved at runtime. The event handler is assigned via an OnClick attribute in the control element in the Web form.
Deane
A: 

You might have a disconnect in your control execution lifecycle because of the move.

You said that you move the control during Page_Load, which happens AFTER postback data is processed, but before postback change notifications and handlers are fired.

Control Execution Lifecycle Reference

Also this says that it is the UniqueID that matters in postback processing, but I'm not sure about events.

Josh