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. Set Focus

Set Focus

Scheduled Pinned Locked Moved ASP.NET
questioncsharphtmlasp-net
3 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.
  • B Offline
    B Offline
    Britnt7
    wrote on last edited by
    #1

    I am trying to set focus to a textbox when the web form is loaded. How can I do this? Can I write it in VB.Net or do I have to use HTML with ASP.Net. I'm not too familiar with HTML, could you please give an example.:doh: Thanks in advance, Beginner in VB and ASP.Net

    D 1 Reply Last reply
    0
    • B Britnt7

      I am trying to set focus to a textbox when the web form is loaded. How can I do this? Can I write it in VB.Net or do I have to use HTML with ASP.Net. I'm not too familiar with HTML, could you please give an example.:doh: Thanks in advance, Beginner in VB and ASP.Net

      D Offline
      D Offline
      David Flores
      wrote on last edited by
      #2

      There are lots of different ways to do it, but here is a function that you can use for setting the focus on loading of page.

      '"name" is the control's ID
      Private Sub FocusOnControl(ByVal name As String)
          Dim strBuilder As String = ""
          strBuilder += "<script language='javascript'>"
          strBuilder += "document.getElementById('" + name + "').focus();"
          strBuilder += "</script>"
          RegisterStartupScript("FocusOnControl", strBuilder)
      End Sub
      

      Then in say in your Page_Load you can call the function

      Private Sub Page\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          FocusOnControl("txtBox")
      End Sub
      
      B 1 Reply Last reply
      0
      • D David Flores

        There are lots of different ways to do it, but here is a function that you can use for setting the focus on loading of page.

        '"name" is the control's ID
        Private Sub FocusOnControl(ByVal name As String)
            Dim strBuilder As String = ""
            strBuilder += "<script language='javascript'>"
            strBuilder += "document.getElementById('" + name + "').focus();"
            strBuilder += "</script>"
            RegisterStartupScript("FocusOnControl", strBuilder)
        End Sub
        

        Then in say in your Page_Load you can call the function

        Private Sub Page\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            FocusOnControl("txtBox")
        End Sub
        
        B Offline
        B Offline
        Britnt7
        wrote on last edited by
        #3

        That worked great!:-D Thanks:cool: Beginner in VB and ASP.Net

        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