[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>

 

 

31 Comments

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

    Login1.Focus();

  • beautifully written. really well described.

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

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

  • thanx alot.....great work

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

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

  • it works
    thanks,
    madu

  • great guide, fixed my bug in a sec!
    thanks :)

  • 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.

  • You rock! This makes the control useful again!

  • 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.

  • it...sort of worked. There's no postback on now, but when its anonymous I want to fire the login button, when its authenticated, I want 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;
    }

  • Thanks! It was a great help for me!

  • nice site it helps me. i m very happy

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

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

    Thanks again.

  • Thanks! This really helped me out!

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

  • 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.

  • 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;
    }

  • Oh, thanks! It helps me!
    Kind regards from Russia!

  • Thanks a lot, you made my day!

  • 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

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

  • 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?

  • 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


  • 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............

  • 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.

  • 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

  • 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.

  • 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.

Comments have been disabled for this content.