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
4 Posts 3 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.
  • C Offline
    C Offline
    chintan1979
    wrote on last edited by
    #1

    Hi, I have a Textbox on my form(Web Control). On the page load I want to setfocus to this textbox. I have tried out following lines of code in body onload one by one, but its not working. 1) document.all['TXTDESC'].focus() 2) window.document.all['TXTDESC'].focus() 3) document.form1.TXTDESC.focus() Chintan

    F 1 Reply Last reply
    0
    • C chintan1979

      Hi, I have a Textbox on my form(Web Control). On the page load I want to setfocus to this textbox. I have tried out following lines of code in body onload one by one, but its not working. 1) document.all['TXTDESC'].focus() 2) window.document.all['TXTDESC'].focus() 3) document.form1.TXTDESC.focus() Chintan

      F Offline
      F Offline
      fadee
      wrote on last edited by
      #2

      Well depends upon the context. JavaScripting? document.Form1.TXTDESC.focus() has to work. Note the F is capital. JavaScript is case sensative. If you are working on ASP .NET, then Form1 is most likely to be the name of the only form. mE! ------------------- Therez No Place like ... 127.0.0.1

      C 1 Reply Last reply
      0
      • F fadee

        Well depends upon the context. JavaScripting? document.Form1.TXTDESC.focus() has to work. Note the F is capital. JavaScript is case sensative. If you are working on ASP .NET, then Form1 is most likely to be the name of the only form. mE! ------------------- Therez No Place like ... 127.0.0.1

        C Offline
        C Offline
        chintan1979
        wrote on last edited by
        #3

        Hi, Thanx for the replay. But I am getting this error while runing the code. Error Message: BC30456: 'document' is not a member of 'ASP.community_aspx'. Source Error: Line 21: Line 22: Chintan

        D 1 Reply Last reply
        0
        • C chintan1979

          Hi, Thanx for the replay. But I am getting this error while runing the code. Error Message: BC30456: 'document' is not a member of 'ASP.community_aspx'. Source Error: Line 21: Line 22: Chintan

          D Offline
          D Offline
          Dr_X
          wrote on last edited by
          #4

          Here use this. This does the trick for me. I put the function in my middle tier so all the pages can use it. Hence, that is why I am passing the page object.

          'In a module or in an inherited class put the following constants.
          Public Const C_ES As String = ""
          Public Const JS_ScriptBegin As String = ""
          Public Const JS_ScriptEnd As String = ""
          Public Const JS_CtrlSetFocusBegin As String = JS_ScriptBegin _
          & "document.getElementById('"
          Public Const JS_CtrlSetFocusEnd As String = "').focus();" _
          & JS_ScriptEnd

          Public Const JS_CtrlSelectBegin As String = JS_ScriptBegin _
          & "document.getElementById('"
          Public Const JS_CtrlSelectEnd As String = "').select();" _
          & JS_ScriptEnd

          'In the Business Object Tier or in the page itself put the following sub
          Friend Sub SetFocus(ByVal Ctrl As Control, ByVal WebPage As Page)
          Try
          If Not Ctrl Is Nothing Then
          Dim ctrlID As String = Ctrl.ID
          If Not ctrlID.Equals(C_ES) Then
          WebPage.RegisterStartupScript("Focus", JS_CtrlSetFocusBegin & ctrlID & JS_CtrlSetFocusEnd)
          'If the control is a TextBox select all of the text.
          If TypeOf Ctrl Is TextBox Then
          WebPage.RegisterStartupScript("Select", JS_CtrlSelectBegin & ctrlID & JS_CtrlSelectEnd)
          End If
          End If
          End If

              Catch ex As Exception
                Throw ex
              End Try
            End Sub
          
            'set focus
            'To call the sub with it being in the Busines Ojbect, BO, use the following
            BO.SetFocus(ddlOffice, Me)
            'To call the sub while it is residing in the page class use the following
            SetFocus(ddlOffice, Me)
          

          Michael I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)

          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