View Light

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!

Rating: (You must be logged in to vote)
Reply