Set default button in Asp.Net pages and in asp:login form

Saturday, 3 October 2009 15:32 by myro

Setting the default botton that needs to be pushed when the Enter key is pressed into an ASPX page, can be implemented easly in Asp.Net.
Imagine that your page holds 2 controls:

  • a Asp.net TextBox named tb1
  • a Asp.net Button named btn1

To instruct the page, to point the default button  on btn1, you should use:

Page.Form.DefaultButton = btn1.ClientID;

Setting the focus on tb1 can be accomplished by:

Page.Form.DefaultFocus = tb1.ClientID;

As you can see, you have to use control's ClientID and not  the control's ID.

But what happens if you want to set the Page's DefaultButton property to a login button contained in a Login form control? Consider surrounding the <asp:Login /> control with a simple asp.net Panel:

<asp:Panel ID="Panel1" runat="server" Height="100%" Width="100%" DefaultButton="llogin$LoginButton">
    <asp:Login ID="llogin" runat="server"
         PasswordRecoveryUrl="~/passwordrecover.aspx"
         CreateUserUrl="~/register.aspx"
         TitleTextStyle-CssClass="contentGroupHeader"
         OnLoggedIn="SetCookie"  >
    <LoginButtonStyle CssClass="button" ></LoginButtonStyle>
    </asp:Login>
</asp:Panel>

...and  set the DefaultButton property using the syntax provided in the example.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:  
Categories:   Web
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed
If you consider this post usefull for your purposes, please consider visiting my sponsors to help me out with the myrocode.com maintenance. Thank you.