set focus to textbox after click event- tried everything please please help!

Discussion in 'ASP .Net' started by JC, Jul 7, 2004.

  1. JC

    JC Guest

    Hello-
    I have a form which has a listbox, a textbox and a submit button. The
    user types a phone number into the textbox, clicks button, text gets
    added to the listbox. After this process I need the focus to be back
    in the textbox. Sounds easy hu?
    Here's my code-

    Private Sub cmdAddLine_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles cmdAddLine.Click

    lstLines.Items.Add(PhoneFormat(txtAddLine.Text))
    txtAddLine.Text = ""
    setFocus(txtAddLine)

    End Sub

    Public Sub setFocus(ByVal ctrl As System.Web.UI.Control)

    Dim strS As String
    strS = "<SCRIPT language='javascript'>document.getElementById('" +
    ctrl.ID + "').focus();</SCRIPT>"
    RegisterStartupScript("focus", strS)

    End Sub

    This will not place the focus in the textbox. What am I missing?

    Again, PLEASE help. Thanks much!
     
    JC, Jul 7, 2004
    #1
    1. Advertising

  2. On 7 Jul 2004 10:42:20 -0700, JC <> wrote:

    > Hello-
    > I have a form which has a listbox, a textbox and a submit button. The
    > user types a phone number into the textbox, clicks button, text gets
    > added to the listbox. After this process I need the focus to be back
    > in the textbox. Sounds easy hu?
    > Here's my code-
    >
    > Private Sub cmdAddLine_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles cmdAddLine.Click
    >
    > lstLines.Items.Add(PhoneFormat(txtAddLine.Text))
    > txtAddLine.Text = ""
    > setFocus(txtAddLine)
    >
    > End Sub
    >
    > Public Sub setFocus(ByVal ctrl As System.Web.UI.Control)
    >
    > Dim strS As String
    > strS = "<SCRIPT language='javascript'>document.getElementById('" +
    > ctrl.ID + "').focus();</SCRIPT>"
    > RegisterStartupScript("focus", strS)
    >
    > End Sub
    >
    > This will not place the focus in the textbox. What am I missing?
    >
    > Again, PLEASE help. Thanks much!


    Do you get a JS error? I assume you are; your control will get a unique
    client ID that .NET creates on the actual HTML, so you can't just look for
    ctrl.ID. Try ctrl.ClientID

    http://msdn.microsoft.com/library/en-us/cpguide/html/cpconpropertiesinwebformscontrols.asp

    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
     
    Craig Deelsnyder, Jul 7, 2004
    #2
    1. Advertising

  3. gah, dang sig...ignore, just testing/updating my sig correctly..

    --
    Craig Deelsnyder
    Microsoft MVP - ASP/ASP.NET
     
    Craig Deelsnyder, Jul 7, 2004
    #3
  4. JC

    Guest

    On 7 Jul 2004 10:42:20 -0700, (JC) wrote:

    >Hello-
    >I have a form which has a listbox, a textbox and a submit button. The
    >user types a phone number into the textbox, clicks button, text gets
    >added to the listbox. After this process I need the focus to be back
    >in the textbox. Sounds easy hu?
    >Here's my code-
    >
    >Private Sub cmdAddLine_Click(ByVal sender As System.Object, ByVal e As
    >System.EventArgs) Handles cmdAddLine.Click
    >
    >lstLines.Items.Add(PhoneFormat(txtAddLine.Text))
    >txtAddLine.Text = ""
    >setFocus(txtAddLine)
    >
    >End Sub
    >
    >Public Sub setFocus(ByVal ctrl As System.Web.UI.Control)
    >
    >Dim strS As String
    >strS = "<SCRIPT language='javascript'>document.getElementById('" +
    >ctrl.ID + "').focus();</SCRIPT>"
    >RegisterStartupScript("focus", strS)
    >
    >End Sub
    >
    >This will not place the focus in the textbox. What am I missing?
    >
    >Again, PLEASE help. Thanks much!


    Put a blank label at the bottom of the form and set its text property
    in the codebehind instead of using the registerstartupscript.

    -Adam
     
    , Jul 7, 2004
    #4
  5. JC

    Jeff C Guest

    :eek:) You're all wrong. I took and made a clean application and it
    worked, so I knew it wasn't the code there. I went back to my app,
    (made a copy) and proceded to strip everything out until it worked.
    Well, after everything was gone it still didn't work. There was only
    one remaining thing, smartnavigaion = true.
    That fixed the focus problem by turning it off, however it introduced a
    whole new set of problems.
    Having a database application, the users need to not use the back
    button. I've got all the standard code for this but it's still not as
    clean as smartnav.
    Is there ANY way to overcome smartnav's focus problem?
    MICROSOFT????
    I didn't think so.
    Anyhow, I've solved one problem only to intoduce many more. I may have
    more ?'s soon, some really good ones I've been working on. Thanks for
    the help so far.
    Jeff



    *** Sent via Devdex http://www.devdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Jeff C, Jul 8, 2004
    #5
    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. KathyB
    Replies:
    0
    Views:
    343
    KathyB
    Jul 23, 2003
  2. Stephen Miller
    Replies:
    4
    Views:
    531
  3. Amil Hanish

    No debug on Server 2003...tried everything

    Amil Hanish, Feb 16, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    1,442
    =?Utf-8?B?R3VuVGFyUw==?=
    Mar 3, 2004
  4. shapper
    Replies:
    0
    Views:
    278
    shapper
    Dec 15, 2006
  5. shapper
    Replies:
    0
    Views:
    259
    shapper
    Dec 15, 2006
Loading...

Share This Page