HOWTO: Set the default button on a page using a Master Page

A little bit more verbose than the old non-MasterPage way of setting a default button (Meaning the button that will receive a click event when the Enter key is pressed on the form), but still easy to do:

Say you have a button called btnStartTrial. The old way would have been simply the following:

this.Form.DefaultButton(btnStartTrial); // Expects a control implementing IButtonControl

However, with a Web Content Form using a Master page, we've got to walk through the Master page object to get to the Default button. Also, since the controls in a Web Content Form may have their ID changed by ASP.NET before it is rendered, we've got to refer to the UniqueID of the button control to ensure that the button can be located at runtime. The code to set the default button in this case is:

this.Master.Page.Form.DefaultButton = btnStartTrial.UniqueID; // Expects a string value.

Note that DefaultButton now expects a string value rather than a control implementing IButtonControl.

That should be all you need to know, enjoy!

Reply
This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available in our efforts to advance understanding of environmental, political, human rights, economic, democracy, scientific, and social justice issues, etc. We believe this constitutes a 'fair use' of any such copyrighted material as provided for in section 107 of the US Copyright Law. In accordance with Title 17 U.S.C. Section 107, the material on this site is distributed without profit to those who have expressed a prior interest in receiving the included information for research and educational purposes. For more information go to: http://www.law.cornell.edu/uscode/17/107.shtml . If you wish to use copyrighted material from this site for purposes of your own that go beyond 'fair use', you must obtain permission from the copyright owner.