Setting the default button for a nested control in ASP.NET

1 minute read

In ASP.NET 2.0, you can declaratively set which button which be “clicked” when the user presses the Enter key. This can be done on either the <form> or <asp:Panel> tag, as follows:

<asp:Panel runat="server" DefaultButton="btnSubmit">
	<asp:TextBox runat="server" ID="txtFirstName" />
	<asp:Button runat="server" ID="btnSubmit" Text="Submit" />

This works fine as long as the button control is directly contained within the Panel or form. However, if the button control is nested within a user control or any other control that implements INamingContainer, it will no longer work.

I discovered that you can still do this declaratively - but you need to use the relative ID. For the sake of argument, let’s say that the button is contained within a CreateUserWizard control (this could be any control or user control though).

For this scenario, you’d need to set the DefaultButton property to the path relative to the Panel on which you’re setting the DefaultButton. For example (note that “__CustomNav0” might need to be replaced - I get this ID because of a custom adapter I use on the CreateUserWizard control):

<asp:Panel runat="server" DefaultButton="cuwRegister$__CustomNav0$btnSubmit">
	<asp:CreateUserWizard runat="server" ID="cuwRegister">
			<asp:CompleteWizardStep runat="server">
					<asp:Button runat="server" ID="btnSubmit" Text="Submit" />

I hope this helps somebody.


Leave a Comment