set focus (Javascript)

Discussion in 'ASP .Net' started by slinky, Jun 4, 2008.

  1. slinky

    slinky Guest

    I'm trying to simply set the focus of "txtUserName" upon opening this
    login form. I put in some Javascript but it is not working. Any
    ideas? Thanks!

    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="Login.aspx.vb"
    Inherits="Forsyth.HR_ReportingTool.UI.Login" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>Login</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/
    intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout" bgcolor="#6699ff"
    background="file:///C:\Inetpub\wwwroot\HR_ReportingTool\vignette.gif">

    <script language="JavaScript">
    function doFocus(){

    if(document.forms[0].txtUserName)document.forms[0].txtUserName.focus();
    }
    </script>

    <form id="Form1" method="post" runat="server">
    <asp:TextBox id="txtUserName"
    </asp:TextBox>
    </form>
    </body>
    </HTML>
    slinky, Jun 4, 2008
    #1
    1. Advertising

  2. 1. Where do you actually call the DoFocus()?
    2. asp:TextBox seems to be not closed properly.

    George.

    "slinky" <> wrote in message
    news:...
    > I'm trying to simply set the focus of "txtUserName" upon opening this
    > login form. I put in some Javascript but it is not working. Any
    > ideas? Thanks!
    >
    > <%@ Page Language="vb" AutoEventWireup="false"
    > Codebehind="Login.aspx.vb"
    > Inherits="Forsyth.HR_ReportingTool.UI.Login" %>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > <HTML>
    > <HEAD>
    > <title>Login</title>
    > <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    > <meta name="vs_defaultClientScript" content="JavaScript">
    > <meta name="vs_targetSchema" content="http://schemas.microsoft.com/
    > intellisense/ie5">
    > </HEAD>
    > <body MS_POSITIONING="GridLayout" bgcolor="#6699ff"
    > background="file:///C:\Inetpub\wwwroot\HR_ReportingTool\vignette.gif">
    >
    > <script language="JavaScript">
    > function doFocus(){
    >
    > if(document.forms[0].txtUserName)document.forms[0].txtUserName.focus();
    > }
    > </script>
    >
    > <form id="Form1" method="post" runat="server">
    > <asp:TextBox id="txtUserName"
    > </asp:TextBox>
    > </form>
    > </body>
    > </HTML>
    George Ter-Saakov, Jun 4, 2008
    #2
    1. Advertising

  3. slinky

    slinky Guest

    I used this:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    txtUserName.focus()
    End Sub

    But get this: 'focus' is not a member of
    'System.Web.UI.webcontrols.TextBox'



    On Jun 4, 4:08 pm, "Matthew Wells" <>
    wrote:
    > You don't need JavaScript...Just put
    >
    > txtUserName.focus();
    >
    > in your page_load event.
    >
    > Matthew Wells
    >
    >
    > "slinky" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > I'm trying to simply set the focus of "txtUserName" upon opening this
    > > login form. I put in some Javascript but it is not working. Any
    > > ideas?   Thanks!

    >
    > > <%@ Page Language="vb" AutoEventWireup="false"
    > > Codebehind="Login.aspx.vb"
    > > Inherits="Forsyth.HR_ReportingTool.UI.Login" %>
    > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > > <HTML>
    > > <HEAD>
    > > <title>Login</title>
    > > <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    > > <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    > > <meta name="vs_defaultClientScript" content="JavaScript">
    > > <meta name="vs_targetSchema" content="http://schemas.microsoft.com/
    > > intellisense/ie5">
    > > </HEAD>
    > > <body  MS_POSITIONING="GridLayout" bgcolor="#6699ff"
    > > background="file:///C:\Inetpub\wwwroot\HR_ReportingTool\vignette.gif">

    >
    > > <script language="JavaScript">
    > > function doFocus(){

    >
    > > if(document.forms[0].txtUserName)document.forms[0].txtUserName.focus();
    > > }
    > > </script>

    >
    > > <form id="Form1" method="post" runat="server">
    > > <asp:TextBox id="txtUserName"
    > > </asp:TextBox>
    > > </form>
    > > </body>
    > > </HTML>- Hide quoted text -

    >
    > - Show quoted text -
    slinky, Jun 5, 2008
    #3
  4. slinky

    slinky Guest

    Yes I'm using code-behind and the Intellisense did not list ".focus()"

    On Jun 5, 9:23 am, "Matthew Wells" <>
    wrote:
    > That surprised me.  When you typed txtUserName., did you get intellisense
    > for .focus())?  I tested it before I repled to you.  I am using C#, but that
    > shouldn't make a difference.  Are you using a code-behind page?
    >
    > "slinky" <> wrote in message
    >
    > news:...
    > I used this:
    >
    >     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    >         txtUserName.focus()
    >     End Sub
    >
    > But get this: 'focus' is not a member of
    > 'System.Web.UI.webcontrols.TextBox'
    >
    > On Jun 4, 4:08 pm, "Matthew Wells" <>
    > wrote:
    >
    >
    >
    > > You don't need JavaScript...Just put

    >
    > > txtUserName.focus();

    >
    > > in your page_load event.

    >
    > > Matthew Wells
    > >

    >
    > > "slinky" <> wrote in message

    >
    > >news:...

    >
    > > > I'm trying to simply set the focus of "txtUserName" upon opening this
    > > > login form. I put in some Javascript but it is not working. Any
    > > > ideas? Thanks!

    >
    > > > <%@ Page Language="vb" AutoEventWireup="false"
    > > > Codebehind="Login.aspx.vb"
    > > > Inherits="Forsyth.HR_ReportingTool.UI.Login" %>
    > > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > > > <HTML>
    > > > <HEAD>
    > > > <title>Login</title>
    > > > <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    > > > <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    > > > <meta name="vs_defaultClientScript" content="JavaScript">
    > > > <meta name="vs_targetSchema" content="http://schemas.microsoft.com/
    > > > intellisense/ie5">
    > > > </HEAD>
    > > > <body MS_POSITIONING="GridLayout" bgcolor="#6699ff"
    > > > background="file:///C:\Inetpub\wwwroot\HR_ReportingTool\vignette.gif">

    >
    > > > <script language="JavaScript">
    > > > function doFocus(){

    >
    > > > if(document.forms[0].txtUserName)document.forms[0].txtUserName.focus();
    > > > }
    > > > </script>

    >
    > > > <form id="Form1" method="post" runat="server">
    > > > <asp:TextBox id="txtUserName"
    > > > </asp:TextBox>
    > > > </form>
    > > > </body>
    > > > </HTML>- Hide quoted text -

    >
    > > - Show quoted text -- Hide quoted text -

    >
    > - Show quoted text -
    slinky, Jun 5, 2008
    #4
  5. slinky

    slinky Guest

    Sorry, should have noted that I'm using VB.net. But no Intellisense
    is showing as a .Focus()
    This is a weird one. Focus had always been so simple in VB 6.0

    On Jun 5, 11:46 am, "Mark Rae [MVP]" <> wrote:
    > "Matthew Wells" <> wrote in message
    >
    > news:p...
    >
    > [top-posting corrected]
    >
    > > That surprised me.  When you typed txtUserName., did you get intellisense
    > > for .focus())?  I tested it before I repled to you.  I am using C#, but
    > > that shouldn't make a difference.  Are you using a code-behind page?

    >
    > If you're using C#, .focus() certainly won't work nor will it show in
    > IntelliSense because C# is case-sensitive...
    >
    > txtUserName.Focus() should work, though...
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net
    slinky, Jun 5, 2008
    #5
  6. re:
    !> This is a weird one. Focus had always been so simple in VB 6.0

    It is, still, simple.

    If you have a textbox named txtUserName in a VB aspx page,
    when you go to the code-behind page and create a Sub Page_Load:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    and then type txtUsername inside it, *and place a dot (.) after that*,
    you should get a dropdown which includes Focus as one of the options.

    The process is the same when you use inline code:

    You create :

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    End Sub

    ....and when you write, or select, txtUsername.

    you'll get a dropdown which includes Focus as one of the options.




    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/
    ======================================
    "slinky" <> wrote in message
    news:...
    Sorry, should have noted that I'm using VB.net. But no Intellisense
    is showing as a .Focus()
    This is a weird one. Focus had always been so simple in VB 6.0

    On Jun 5, 11:46 am, "Mark Rae [MVP]" <> wrote:
    > "Matthew Wells" <> wrote in message
    >
    > news:p...
    >
    > [top-posting corrected]
    >
    > > That surprised me. When you typed txtUserName., did you get intellisense
    > > for .focus())? I tested it before I repled to you. I am using C#, but
    > > that shouldn't make a difference. Are you using a code-behind page?

    >
    > If you're using C#, .focus() certainly won't work nor will it show in
    > IntelliSense because C# is case-sensitive...
    >
    > txtUserName.Focus() should work, though...
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net
    Juan T. Llibre, Jun 5, 2008
    #6
  7. slinky

    slinky Guest

    I did exactly that, and still no Focus as a choice or accepted
    keyword:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    txtUserName.
    End Sub

    I double-checked the spelling, etc.


    On Jun 5, 12:33 pm, "Juan T. Llibre" <>
    wrote:
    > re:
    > !> This is a weird one. Focus had always been so simple in VB 6.0
    >
    > It is, still, simple.
    >
    > If you have a textbox named txtUserName in a VB aspx page,
    > when you go to the code-behind page and create a Sub Page_Load:
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    >
    > End Sub
    >
    > and then type txtUsername inside it, *and place a dot (.) after that*,
    > you should get a dropdown which includes Focus as one of the options.
    >
    > The process is the same when you use inline code:
    >
    > You create :
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    > End Sub
    >
    > ...and when you write, or select, txtUsername.
    >
    > you'll get a dropdown which includes Focus as one of the options.
    >
    > 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/
    > ======================================"slinky" <> wrote in message
    >
    > news:...
    > Sorry, should have noted that I'm using VB.net.   But no Intellisense
    > is showing as a .Focus()
    > This is a weird one. Focus had always been so simple in VB 6.0
    >
    > On Jun 5, 11:46 am, "Mark Rae [MVP]" <> wrote:
    >
    >
    >
    > > "Matthew Wells" <> wrote in message

    >
    > >news:p...

    >
    > > [top-posting corrected]

    >
    > > > That surprised me. When you typed txtUserName., did you get intellisense
    > > > for .focus())? I tested it before I repled to you. I am using C#, but
    > > > that shouldn't make a difference. Are you using a code-behind page?

    >
    > > If you're using C#, .focus() certainly won't work nor will it show in
    > > IntelliSense because C# is case-sensitive...

    >
    > > txtUserName.Focus() should work, though...

    >
    > > --
    > > Mark Rae
    > > ASP.NET MVPhttp://www.markrae.net- Hide quoted text -

    >
    > - Show quoted text -
    slinky, Jun 5, 2008
    #7
  8. re:
    !> I did exactly that, and still no Focus as a choice or accepted keyword:

    That's odd.

    I just checked with both VS 2005 and VS 2008
    ....and I get Intellisense for Focus with both.


    1. Which version of VS are you using ?

    2. Do you actually have a textbox named txtUserName ?

    I first tested with the standard "TextBox1" ID suggested by the IDE,
    and then created a texbox with the ID "txtUserName",
    and got the Intellisense dropdown with the Focus option for both.




    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/
    ======================================
    "slinky" <> wrote in message
    news:...
    I did exactly that, and still no Focus as a choice or accepted keyword:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    txtUserName.
    End Sub

    I double-checked the spelling, etc.


    On Jun 5, 12:33 pm, "Juan T. Llibre" <>
    wrote:
    > re:
    > !> This is a weird one. Focus had always been so simple in VB 6.0
    >
    > It is, still, simple.
    >
    > If you have a textbox named txtUserName in a VB aspx page,
    > when you go to the code-behind page and create a Sub Page_Load:
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    >
    > End Sub
    >
    > and then type txtUsername inside it, *and place a dot (.) after that*,
    > you should get a dropdown which includes Focus as one of the options.
    >
    > The process is the same when you use inline code:
    >
    > You create :
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    > End Sub
    >
    > ...and when you write, or select, txtUsername.
    >
    > you'll get a dropdown which includes Focus as one of the options.
    >
    > 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/
    > ======================================"slinky" <> wrote in message
    >
    > news:...
    > Sorry, should have noted that I'm using VB.net. But no Intellisense
    > is showing as a .Focus()
    > This is a weird one. Focus had always been so simple in VB 6.0
    >
    > On Jun 5, 11:46 am, "Mark Rae [MVP]" <> wrote:
    >
    >
    >
    > > "Matthew Wells" <> wrote in message

    >
    > >news:p...

    >
    > > [top-posting corrected]

    >
    > > > That surprised me. When you typed txtUserName., did you get intellisense
    > > > for .focus())? I tested it before I repled to you. I am using C#, but
    > > > that shouldn't make a difference. Are you using a code-behind page?

    >
    > > If you're using C#, .focus() certainly won't work nor will it show in
    > > IntelliSense because C# is case-sensitive...

    >
    > > txtUserName.Focus() should work, though...

    >
    > > --
    > > Mark Rae
    > > ASP.NET MVPhttp://www.markrae.net- Hide quoted text -

    >
    > - Show quoted text -
    Juan T. Llibre, Jun 5, 2008
    #8
  9. slinky

    slinky Guest

    VS 2003 Version 7.1.6030
    .net framework 1.1 Version 1.1.4322 SP1

    yes definitely have a textbox on my .aspx named txtUserName

    On Jun 5, 1:38 pm, "Juan T. Llibre" <> wrote:
    > re:
    > !> I did exactly that, and still no Focus as a choice or accepted keyword:
    >
    > That's odd.
    >
    > I just checked with both VS 2005 and VS 2008
    > ...and I get Intellisense for Focus with both.
    >
    > 1. Which version of VS are you using ?
    >
    > 2. Do you actually have a textbox named txtUserName ?
    >
    > I first tested with the standard "TextBox1" ID suggested by the IDE,
    > and then created a texbox with the ID "txtUserName",
    > and got the Intellisense dropdown with the Focus option for both.
    >
    > 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/
    > ======================================"slinky" <> wrote in message
    >
    > news:...
    > I did exactly that, and still no Focus as a choice or accepted keyword:
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    >  txtUserName.
    > End Sub
    >
    > I double-checked the spelling, etc.
    >
    > On Jun 5, 12:33 pm, "Juan T. Llibre" <>
    > wrote:
    >
    >
    >
    > > re:
    > > !> This is a weird one. Focus had always been so simple in VB 6.0

    >
    > > It is, still, simple.

    >
    > > If you have a textbox named txtUserName in a VB aspx page,
    > > when you go to the code-behind page and create a Sub Page_Load:

    >
    > > Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    >
    > > End Sub

    >
    > > and then type txtUsername inside it, *and place a dot (.) after that*,
    > > you should get a dropdown which includes Focus as one of the options.

    >
    > > The process is the same when you use inline code:

    >
    > > You create :

    >
    > > Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    > > End Sub

    >
    > > ...and when you write, or select, txtUsername.

    >
    > > you'll get a dropdown which includes Focus as one of the options.

    >
    > > 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/
    > > ======================================"slinky" <> wrote in message

    >
    > >news:...
    > > Sorry, should have noted that I'm using VB.net. But no Intellisense
    > > is showing as a .Focus()
    > > This is a weird one. Focus had always been so simple in VB 6.0

    >
    > > On Jun 5, 11:46 am, "Mark Rae [MVP]" <> wrote:

    >
    > > > "Matthew Wells" <> wrote in message

    >
    > > >news:p...

    >
    > > > [top-posting corrected]

    >
    > > > > That surprised me. When you typed txtUserName., did you get intellisense
    > > > > for .focus())? I tested it before I repled to you. I am using C#, but
    > > > > that shouldn't make a difference. Are you using a code-behind page?

    >
    > > > If you're using C#, .focus() certainly won't work nor will it show in
    > > > IntelliSense because C# is case-sensitive...

    >
    > > > txtUserName.Focus() should work, though...

    >
    > > > --
    > > > Mark Rae
    > > > ASP.NET MVPhttp://www.markrae.net-Hide quoted text -

    >
    > > - Show quoted text -- Hide quoted text -

    >
    > - Show quoted text -
    slinky, Jun 5, 2008
    #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. feng
    Replies:
    5
    Views:
    243,209
    Rajiv R
    Feb 27, 2004
  2. Joey
    Replies:
    4
    Views:
    144,388
    Adrian G
    Sep 6, 2011
  3. Manav
    Replies:
    0
    Views:
    763
    Manav
    Oct 15, 2003
  4. Replies:
    2
    Views:
    325
    Thomas 'PointedEars' Lahn
    Jun 6, 2005
  5. Roger
    Replies:
    3
    Views:
    306
Loading...

Share This Page