Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Setting the focus on control jumps page to the top

Setting the focus on control jumps page to the top

Scheduled Pinned Locked Moved ASP.NET
databasehelpquestionannouncement
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jay Royall
    wrote on last edited by
    #1

    Hi, My senario: I have a sign-up form which, amoungst others, has a username textbox. The username textbox has autopostback set to true which, when posted back, checks to see if the entered username already exists in the DB and displays a message if so. This works fine. But the problem I have is that in the code behind I am setting the focus to a control after doing the username check ( using txtUsername.Focus() ). But doing this causes the page to jump back to the top. This doesn't happen if I don't give a control the focus. Any ideas how I can solve this? A little more info about my form just in case it's relevant. The form is in an AJAX Accodion control which is in an AJAX Update Panel. Thanks :)

    A 1 Reply Last reply
    0
    • J Jay Royall

      Hi, My senario: I have a sign-up form which, amoungst others, has a username textbox. The username textbox has autopostback set to true which, when posted back, checks to see if the entered username already exists in the DB and displays a message if so. This works fine. But the problem I have is that in the code behind I am setting the focus to a control after doing the username check ( using txtUsername.Focus() ). But doing this causes the page to jump back to the top. This doesn't happen if I don't give a control the focus. Any ideas how I can solve this? A little more info about my form just in case it's relevant. The form is in an AJAX Accodion control which is in an AJAX Update Panel. Thanks :)

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      Why dont you use

      document.getElementById('textbox').focus();

      in onload of the page ?? :confused:

      Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


      My Latest Articles-->** Simplify Code Using NDepend
      Basics of Bing Search API using .NET
      Microsoft Bing MAP using Javascript

      J 1 Reply Last reply
      0
      • A Abhishek Sur

        Why dont you use

        document.getElementById('textbox').focus();

        in onload of the page ?? :confused:

        Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


        My Latest Articles-->** Simplify Code Using NDepend
        Basics of Bing Search API using .NET
        Microsoft Bing MAP using Javascript

        J Offline
        J Offline
        Jay Royall
        wrote on last edited by
        #3

        Wouldn't that set the focus to that particular textbox every time the page was loaded? I need to be able to set the focus to a different textbox depending on the result of the username check, i.e. if the check fails then set focus to txtUsername but if it passes then set focus to txtPassword. But also, when the page is loaded for the first time then the focus needs to be set to yet a different control. So using Javascript how would I determine which control to give the focus to? Thanks for help by the way :)

        A 1 Reply Last reply
        0
        • J Jay Royall

          Wouldn't that set the focus to that particular textbox every time the page was loaded? I need to be able to set the focus to a different textbox depending on the result of the username check, i.e. if the check fails then set focus to txtUsername but if it passes then set focus to txtPassword. But also, when the page is loaded for the first time then the focus needs to be set to yet a different control. So using Javascript how would I determine which control to give the focus to? Thanks for help by the way :)

          A Offline
          A Offline
          Abhishek Sur
          wrote on last edited by
          #4

          Well, if the page is actually posted back, you might consider adding a script block using RegisterScriptBlock to the client page to have the proper textbox focus. It is not usually great to do focus in server side in case of ASP.NET. use txtBox.ClientId to get the id on the control to be focus. But If I was in your situation, I would have invoked an AJAX call to the server, and get if it is available or not. It is really unnecessary to fully postback the page for this. ;)

          Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


          My Latest Articles-->** Simplify Code Using NDepend
          Basics of Bing Search API using .NET
          Microsoft Bing MAP using Javascript

          J 1 Reply Last reply
          0
          • A Abhishek Sur

            Well, if the page is actually posted back, you might consider adding a script block using RegisterScriptBlock to the client page to have the proper textbox focus. It is not usually great to do focus in server side in case of ASP.NET. use txtBox.ClientId to get the id on the control to be focus. But If I was in your situation, I would have invoked an AJAX call to the server, and get if it is available or not. It is really unnecessary to fully postback the page for this. ;)

            Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


            My Latest Articles-->** Simplify Code Using NDepend
            Basics of Bing Search API using .NET
            Microsoft Bing MAP using Javascript

            J Offline
            J Offline
            Jay Royall
            wrote on last edited by
            #5

            Abhishek Sur wrote:

            But If I was in your situation, I would have invoked an AJAX call to the server, and get if it is available or not.

            Ah, didn't realise that I could do this (I am a newbie to ASP.NET :D ) Thanks again for your help.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups