2

i have a login control and a create user control i my web page...i want the cursor to be in the user name text box of the login control when the page loads...how can i do that??

<asp:LoginView ID="LoginView1" runat="server">
              <LoggedInTemplate>
                  Bingo..!!! Youuuuu did it...<asp:LoginName ID="LoginName1" runat="server" />.



              </LoggedInTemplate>
              <AnonymousTemplate>

                  <asp:DropShadowExtender ID="DropShadowExtender1" runat="server" 
                                            TargetControlID="Panel1" 
                                            Rounded="true" 
                                            Opacity=".38">
                  </asp:DropShadowExtender>
                  <asp:Panel ID="Panel1" runat="server" 
                                         BackColor="Silver">
                  <asp:Login ID="Login1" runat="server" 
                                         DestinationPageUrl="~/ViewCart_aspx/ViewCart.aspx" 
                                         Height="152px" 
                                         Width="396px" 
                                         RememberMeSet="True" 
                                         OnLoggedIn="ContinueButton_Click" >
                      <LayoutTemplate>
                          <fieldset>
                          <table border="0" 
                                 cellpadding="1" 
                                 cellspacing="0" 
                                 style="border-collapse:collapse;">
                              <tr>
                                  <td>
                                      <table border="0" cellpadding="0" style="height:152px;width:396px;">
                                          <tr>
                                              <td align="center" colspan="2">
                                                 <h3>Log In</h3> </td>
                                          </tr>
                                          <tr>
                                              <td align="right">
                                                  <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                                              </td>
                                              <td>&nbsp;
                                                  <asp:TextBox ID="UserName" runat="server" Width="150px" TabIndex="0"></asp:TextBox>
                                                  <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                                      ControlToValidate="UserName" ErrorMessage="User Name is required." 
                                                      ToolTip="User Name is required." ValidationGroup="ctl01$Login1">*</asp:RequiredFieldValidator>
                                              </td>
                                          </tr>

as u can see the UserName textbox is inside the login control..so i cannot access its property..how do i find the control??

Monodeep
  • 1,364
  • 1
  • 17
  • 36

3 Answers3

2

EDIT: as you mentioned in comments, you want to set your login button to be clicked as a default button. For this you need to set this button as a default button.

Unfortunatelly, you didn't format you code properly as I asked in a comment to your question. So I assume the login button is located in the same name container as the username text box and its name is btnLogin and you could set this control as a default control with HtmlForm.DefaultButton property, so:

You could use Page.SetFocus for this. It sets the browser focus to the specified control:

Page.SetFocus(txtName);

If you want to reach your UserName textbox, you could use just:

var login1 = LoginView1.FindControl("Login1") as Login;
if (login1 != null)
{
    var txtUserName = login1.FindControl("UserName");
    if (txtUserName != null)
    {
        Page.SetFocus(txtUserName);
    }

    var btnLogin = login1.FindControl("btnLogin");
    if (btnLogin != null) 
    {
         Page.Form.DefaultButton = btnLogin.UniqueID;
    }
}

But note:

For the LoginView control, when being added onto a page, at a certain time, only one Template (anonymous or loggedIn ) is applied on the Control instance, so at that time, we can only retrieve the reference of those controls in the active template( can't access those in the non-active template).

Alex
  • 30,696
  • 10
  • 74
  • 131
  • i tried your code..it gives me an error for this `LoginView.FindControl` Error=Reference to a non-shared member requires an object reference. – Monodeep Apr 29 '11 at 15:00
  • @Monodeep, I use `loginView` as the name of the `LoginView` control, not `LoginView` as you mentioned. Now made changes in my answer according to your code sample - changed loginview's name to `LoginView1`. – Alex Apr 29 '11 at 15:11
  • Thanks..:) one more question..after i enter user name and password..when i press enter create user button gets clicked??why??pressing the enter button clicks the create user button...why??how to fix it??i want the login button to be clicked. – Monodeep Apr 29 '11 at 15:34
  • @Monodeep, you need to set login button as a default button – Alex Apr 29 '11 at 16:39
1

Write the code in Page load event like follows

textbox1.focus();

where ever it may be, by using the ID of the control you have to access the control in the code behind.

  • the ID of my textbox is `UserName` so i am using `UserName.Focus()` but it says `UserName` not defined.Because its inside the login control..what to do now?? – Monodeep Apr 29 '11 at 13:16
  • Use the id of Login control itself, it will set the default focus to the first textbox i.e., username automatically. – Sai Kalyan Kumar Akshinthala Apr 29 '11 at 13:23
  • ahhh...actually the login control is inside a login view control's Anonymous template...i have posted the code above...have a look at it. – Monodeep Apr 29 '11 at 13:30
0

The DefaultFocus property seems to be suitable in this case:

// inside page_load, LoginUser is the Login control
Page.Form.DefaultFocus = LoginUser.FindControl("Username").ClientID;

Related question: Set focus to textbox in ASP.NET Login control on page load

Community
  • 1
  • 1
Hoang Tran
  • 481
  • 1
  • 6
  • 13