Can we set focus in UserName of a LoginView

Discussion in 'ASP .Net' started by ad, Sep 21, 2005.

  1. ad

    ad Guest

    I use a LoginView to authenticate user.
    Can we set the focus in the UserName or Password of the LoginView?
    ad, Sep 21, 2005
    #1
    1. Advertising

  2. ad

    Guest

    here is one I found and modified it for csharp:

    private void SetFocusControl(String ControlName)
    {
    //http://www.developer.com/net/asp/article.php/2237431
    //character 34 = "

    String script = "<script language=\"javascript\""+
    ">" +
    " var control = document.getElementById(\"" +
    ControlName + "\");" +
    " if( control != null ){control.focus();}" +
    "</script>";

    Page.RegisterStartupScript("Focus", script);
    }
    , Sep 21, 2005
    #2
    1. Advertising

  3. Juan T. Llibre, Sep 21, 2005
    #3
  4. ad

    ad Guest

    Thanks,
    If the UserName control is general textbox, It can do by SetFocus or
    Javascript.
    But now the UserName is in LoginView.
    How can I do?


    "Juan T. Llibre" <> ¼¶¼g©ó¶l¥ó·s»D:...
    > See :
    >
    > http://ryanfarley.com/blog/archive/2004/12/21/1325.aspx
    >
    >
    >
    > Juan T. Llibre, ASP.NET MVP
    > ASP.NET FAQ : http://asp.net.do/faq/
    > Foros de ASP.NET en Español : http://asp.net.do/foros/
    > ======================================
    > "ad" <> wrote in message
    > news:u3cR%...
    >>I use a LoginView to authenticate user.
    >> Can we set the focus in the UserName or Password of the LoginView?

    >
    >
    ad, Sep 21, 2005
    #4
  5. ad

    jnewquis

    Joined:
    Jun 27, 2006
    Messages:
    1
    Username focus in a LoginView

    I also had this page inside a master.page but this is what worked for me in the PreRender event ...



    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

    'Find the Login control inside the LoginView
    Dim curLogin As System.Web.UI.WebControls.Login = CType(LoginView1.FindControl("Login1"), System.Web.UI.WebControls.Login)

    'Then find the Username textbox control
    Dim txtUsername As TextBox = CType(curLogin.FindControl("UserName"), TextBox)

    'Now set the focus on it
    txtUsername.Focus()
    End Sub
    jnewquis, Jun 27, 2006
    #5
  6. ad

    jostar

    Joined:
    Mar 21, 2007
    Messages:
    1
    C# solution to LoginView SetFocus

    Thanks for the ideas in VB.

    Here is a C# solution that workes for me.

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    if(!User.Identity.IsAuthenticated)
    {
    Login lg = (Login)LoginView1.FindControl("Login1");
    TextBox tb = (TextBox)lg.FindControl("UserName");
    SetFocus(tb);
    }
    }
    }

    BTW notice that tb does not have double quotes around it in the SetFocus (tb). I was pulling my hair out following another thread that had quotes around the control string inside the SetFocus method. If I can find that post again, I'll put in a correction there too!

    John
    jostar, Mar 21, 2007
    #6
  7. ad

    talcott

    Joined:
    Dec 26, 2007
    Messages:
    1
    It works!


    Hi! This really works. Thanks
    talcott, Dec 26, 2007
    #7
  8. ad

    Suresh16

    Joined:
    Mar 17, 2009
    Messages:
    1
    it is working in IE but not working in Firefox.
    I do something like this:
    TextBox usernametxt = (TextBox)this.logOn.FindControl("UserName");
    SetFocus(usernametxt);

    on the aspx page I have code like this....
    <asp:Login ID="logOn" runat="server".......>
    <LayoutTemplate>
    ......
    <asp:TextBox ID="UserName" runat="server" Width="250" TabIndex="1"></asp:TextBox>
    ......
    </LayoutTemplate>
    </asp:Login>

    Can someone help me with this?
    Suresh16, Mar 17, 2009
    #8
  9. ad

    blad3runn69

    Joined:
    Jun 10, 2010
    Messages:
    1
    LoginView.Focus Method

    thank you very much jnewquis, very much appreciated.
    linked to msdn community content - LoginView.Focus Method

    thanks again jnewquis :)
    blad3runn69, Jun 10, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David R.
    Replies:
    1
    Views:
    6,009
    Christopher Reed
    Jan 8, 2006
  2. Jack
    Replies:
    1
    Views:
    865
    Alessandro Zifiglio
    Jul 3, 2006
  3. Shailesh Patel
    Replies:
    0
    Views:
    439
    Shailesh Patel
    Nov 8, 2006
  4. Replies:
    2
    Views:
    329
    Thomas 'PointedEars' Lahn
    Jun 6, 2005
  5. Roger
    Replies:
    3
    Views:
    310
Loading...

Share This Page