开发者

Will moving a control into the master page break event bindings?

Say you h开发者_如何学编程ave 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?


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 :)


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜