[ASP.NET] Setting the DefaultButton for a Login control - Jon Galloway

[ASP.NET] Setting the DefaultButton for a Login control

Background

One underused feature in ASP.NET 2.0 is the ability to set a default button for a form. In the past, this often required some extra JavaScript, and was just enough of a pain that it didn't get done until someone asked for it. The ASP.NET form element has DefaultButton and DefaultFocus properties which do exactly what you'd think. For instance, a site I'm working on has a search bar in the header, so I added the following to the form control. Default focus goes to the search textbox, and pressing the enter key submits the form.

<form id="MainForm" runat="server" DefaultButton="Search" DefaultFocus="SearchText">

In my case, the form is declared in my site's MasterPage, You might not want to do that depending on how many forms are in your site; this site's got very few, so it's simpler to set it globally and handle the exceptions. Unless I say different, every page will focus the search textbox and the search button will handle the enter keypress.

Overriding DefaultButton in Page

There are two few ways to override DefaultButton and DefaultFocus. You can override in your codebehind:

this.Form.DefaultButton = MyButton.UniqueID;

In addition to the Form, you can also set the DefaultButton in an <asp:Panel>. That's handy because you can nest Panels, and Panel with the current focus gets to set the Default button, bubbling up until you get to the form.

Gotcha! Containers mangle control ID's, but that's what DefaultButton requires

I ran into this on a simple login form. When the user's logging in, we want to make sure the default button is set to submit the login form, but since the login button is generated by a template, we don't have an ID to work with. The solution is to reference the parent control's id (the <asp:Login> control), then add the $, then add the ID of the actual control. You can find the ID of the control by viewing source if you're not sure.

<asp:LoginView ID="LoginView" runat="server"> <LoggedInTemplate> <div> You are logged in as <asp:LoginName ID="LoginName" runat="server" /> </div> <div> <asp:LoginStatus ID="LoginStatus" runat="server" /> </div> </LoggedInTemplate> <AnonymousTemplate> <asp:Panel ID="panelLogin" runat="server" DefaultButton="Login$LoginButton"> <asp:Login ID="Login" runat="server" /> </asp:Panel> </AnonymousTemplate> </asp:LoginView>

 

 
Published Wednesday, October 03, 2007 1:57 AM by Jon Galloway
Filed under:

Comments

# Setting the DefaultButton for a Login control

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Wednesday, October 03, 2007 9:18 AM by DotNetKicks.com

# re: [ASP.NET] Setting the DefaultButton for a Login control

I use this is the PageLoad event it seems to work. Much simpler!!

Login1.Focus();

Wednesday, October 03, 2007 10:27 AM by Nandu

# 15 Links Today (2007-10-03)

Pingback from  15 Links Today (2007-10-03)

Wednesday, October 03, 2007 11:20 AM by 15 Links Today (2007-10-03)

# re: [ASP.NET] Setting the DefaultButton for a Login control

beautifully written. really well described.

makes me want to run out and set default buttons everywhere !

Wednesday, October 03, 2007 11:55 PM by secretGeek

# re: [ASP.NET] Setting the DefaultButton for a Login control

I never noticed you could set default buttons on panels. That'll come in handy! :)

Friday, October 05, 2007 9:15 AM by Milan Negovan

# re: [ASP.NET] Setting the DefaultButton for a Login control

thanx alot.....great work

Wednesday, April 16, 2008 12:43 PM by Khan

# re: [ASP.NET] Setting the DefaultButton for a Login control

Really helpful. I've been pulling my hair out over this.

Monday, April 28, 2008 1:22 AM by Nathan

# re: [ASP.NET] Setting the DefaultButton for a Login control

I just found this and implemented:

       <asp:Panel ID="panelLogin" runat="server" DefaultButton="Login1$LoginButton">

         <asp:Login ID="Login1" runat="server" CssClass="logintext" Width="342px" DisplayRememberMe="False"

           TitleText="Login" LoginButtonText="Login">

           <LabelStyle CssClass="logintext" />

         </asp:Login>

       </asp:Panel>

It worked like a charm!!! thank you very much!

Kiley

Wednesday, June 04, 2008 3:28 PM by Kiley

# re: [ASP.NET] Setting the DefaultButton for a Login control

Just when I was about to give up using templates and reinvent the wheel. You came to my rescue, Thanks a lot.

Wednesday, June 25, 2008 3:04 AM by Madia Thomas

# Ultracet.

Ultracet. Ultracet buzz.

Sunday, July 27, 2008 7:25 AM by Ultracet.

# re: [ASP.NET] Setting the DefaultButton for a Login control

Great! Thanks!

Sunday, July 27, 2008 8:11 PM by gretchen

# re: [ASP.NET] Setting the DefaultButton for a Login control

it works

thanks,

madu

Friday, August 08, 2008 6:10 AM by Madu

# re: [ASP.NET] Setting the DefaultButton for a Login control

great guide, fixed my bug in a sec!

thanks :)

Thursday, September 04, 2008 4:30 AM by Lior Saadon

# re: [ASP.NET] Setting the DefaultButton for a Login control

I hope it will help.

protected void Page_Load(object sender, EventArgs e)

   {

       Page.Form.DefaultFocus = ((LoginForm.FindControl("UserName")) as TextBox).UniqueID;

       Page.Form.DefaultButton = ((LoginForm.FindControl("LoginButton")) as Button).UniqueID;

   }

LoginForm is you Login Control ID.

Tuesday, September 09, 2008 2:59 AM by btsbodi

# re: [ASP.NET] Setting the DefaultButton for a Login control

You rock!  This makes the control useful again!

Monday, October 06, 2008 5:12 PM by Joe

# re: [ASP.NET] Setting the DefaultButton for a Login control

How they can overlook this kind of thing when designing these controls is beyond me.  It's simple user interface design.  Sit a damn user in front of it and see what they say!

Thanks for this post.

Thursday, November 27, 2008 2:49 PM by morgan

# re: [ASP.NET] Setting the DefaultButton for a Login control

it...sort of worked.  There's no postback on <enter> now, but when its anonymous I want <enter> to fire the login button, when its authenticated, I want <enter> to fire the search button.  It doesnt work..but it doesnt post back either.

livMaster and livSearch are LoginView controls.

protected void Page_Load(object sender, EventArgs e)

   {

       if (HttpContext.Current.User.Identity.IsAuthenticated == true)

       {

           ImageButton btn = this.livSearch.FindControl("btnSearch") as ImageButton;

           form1.DefaultButton = btn.UniqueID;

       }

       else

       {

           LinkButton btn = this.livMaster.FindControl("btnLogin") as LinkButton;

           form1.DefaultButton = btn.UniqueID;

       }

Tuesday, December 30, 2008 10:58 PM by Matt

# re: [ASP.NET] Setting the DefaultButton for a Login control

Thanks! It was a great help for me!

Wednesday, January 14, 2009 7:36 AM by jeka li

# re: [ASP.NET] Setting the DefaultButton for a Login control

nice site it helps me. i m very happy

Thursday, March 19, 2009 2:14 PM by faisal

Leave a Comment

(required) 
(required) 
(optional)
(required)