[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 3, 2007 1:57 AM by Jon Galloway
Filed under:

Comments

# 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 3, 2007 10:27 AM by Nandu

# 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 3, 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 5, 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

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

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

it works

thanks,

madu

Friday, August 8, 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 4, 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 9, 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 6, 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

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

This is exactly what I was looking for. Thanks very much for posting this article.

Thursday, February 25, 2010 10:22 AM by Monica

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

That was really annoying me. And when I found the solution, I wondered. Also I understood the importance of DefaultButton property..

Thanks again.

Monday, April 12, 2010 3:49 PM by Silverlight.Shailendra

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

Thanks! This really helped me out!

Wednesday, April 14, 2010 1:48 PM by webthaumaturge

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

You guys are too cool. I want to be like you but i can't though I'm trying.

Tuesday, August 17, 2010 2:25 AM by Simran

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

Dude, you ROCK!!! Totally helped me out, for the life of me I could not get this to work until i read and implemented your post. Thanks.

Saturday, September 11, 2010 4:18 PM by Sam

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

Combining some the above, the following solution worked perfectly for me:

Insert in Page_Load

       Login lg = (Login)LoginArea.FindControl("Login1");

       if (lg != null)

       {

       // LoginButton becomes defaultbutton

           ImageButton btnLogin = (ImageButton)lg.FindControl("LoginButton");

           Page.Form.DefaultButton = btnLogin.UniqueID;

       }

Friday, September 24, 2010 5:28 AM by Langarm

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

Oh, thanks! It helps me!

Kind regards from Russia!

Sunday, October 3, 2010 2:19 PM by Eugene

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

Thanks a lot, you made my day!

Thursday, October 21, 2010 8:44 PM by mlandry

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

I am sorry, that has interfered... At me a similar situation. Let's discuss.

Excuse, that I can not participate now in discussion - it is very occupied. But I will return - I will necessarily write that I think on this question.

I do not see your logic

I am very grateful to you for the information.

What charming message

Tuesday, December 14, 2010 11:24 AM by hp pavilion dv5

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

Thanks for putting together the article.  This really helped me a lot.

Monday, March 21, 2011 3:13 AM by bsmith95610

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

This works fine if Java script is enabled.

But if I disable brower's java script, then this does not work.

Anyone else faced the same issue?

Thursday, May 26, 2011 5:33 AM by nirman.doshi

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

thanks, the above code is working fine .But in my Search page some textbox controls should be validate using javascript. when entering the text into textbox and click on pressing  enter key  . it doesn't work. how to do the searching with validations when pressing the enter key.

         Please any one help me

Wednesday, July 18, 2012 1:03 AM by Ch Lokesh

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

thanks, the above code is working fine .But in my Search page some textbox controls should be validate using javascript. when entering the text into textbox and click on pressing  enter key  . It works fine.But validation of textbox doesn't work how to do the searching with validations when pressing the enter key.

for example : we can enter only numbers in  textbox . when set the default button for panel or form in searching web form. how will work validations with default button.

Please any one help me............

Wednesday, July 18, 2012 1:13 AM by Guravaiah C

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

Hair differ in after a long time of duration and bawl out of growth. Longest-living hair on his fore-part - to 4 or uniform 10 years, but the fraction junior to the armpits, eyebrows and eyelashes - only 3-4 months. Japanese ball Hiroko Yamaske took 18 years to reach its braid measure of 2.6 m normal flowering of curls per period - there 0.35-0.4 mm, and at end of day they stem badly, and preferably in the evening. On the head, beard and underarm ringlets grows more actively than in the rest of the body.

Thursday, December 27, 2012 4:14 AM by Seerfrarben

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

Here's a very easy solution for those who want a quick and easy solution:

Button test = LoginUser.Controls[0].FindControl("loginButton") as Button;

The login control contains a single container which can be accessed then searched and the default template's login button is named "loginButton" by default. I don't understand why this has to be so complex. Lol

Tuesday, January 1, 2013 9:24 AM by cantchoos

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

I am happy that all the spyware, adware and Trojans that were

on my computing device before are it for unloosen

and try it easy! The interface is the work on of assembling

computing device-related grounds. One popular Keylogger is

Interlace ikon in the left wing-hand box of the buttocks.

Thursday, February 7, 2013 10:15 PM by Jaramillo

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

executive chairss are intentional in such a way so as to induce an employee sit at his do work post cushions and covers at IKEA, but it toll $60 for the Jazz band.

Wednesday, March 6, 2013 4:34 AM by Linares