Results 1 to 6 of 6

Thread: newB: set focus textbox w/WebControls?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2003
    Location
    CA
    Posts
    16

    newB: set focus textbox w/WebControls?

    Hello all this is my first post and I am new to VB and .NET.

    I would like to set focus to a textbox when my page loads.

    Here is some of what I have:
    Protected WithEvents textbox1 As System.Web.UI.WebControls.TextBox

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

    'Set position of cursor in text box.
    'textbox1.focus 'NOT WORKING??

    I think the problem is with WebControls. Is there another option?
    I don't know about WebControls vs Others. I am trying to modify existing code.

    I have also tried to set the tab order but this did not help.

    Thanks.

  2. #2
    Lively Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    95
    Do you specifically need to use a web control?

    System.Web.UI.WebControls.TextBox does not appear to have an entry for Focus in the help, whereas System.Windows.Forms.TextBox does.

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    You might want to check out googles home page source code. It has some javascript that I think does this.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2003
    Location
    CA
    Posts
    16
    I found this in the ASP .NET forum written by Dr.Who/Tim, it works.

    This will work, except on self-made usercontrols...

    just add a 'imports system.text' line before your class dec.

    add this to your startup:

    Dim strBuilder As StringBuilder = New StringBuilder()

    strBuilder.Append("<script language='javascript'>")
    strBuilder.Append("function setFocus() {")
    strBuilder.Append("document.getElementById('TextBox3').focus();}")
    strBuilder.Append(" window.onload=setFocus;")
    strBuilder.Append("</script>")
    RegisterClientScriptBlock("Focus", strBuilder.ToString)

    Remember to change 'TextBox3' to yourTextBoxId
    If anyone has a better way please post. But this does work!
    Thank you Dr.Who

  5. #5

  6. #6
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    Keep in mind server side controls can't handle a lot of the logic that you are accustomed to using in VB. It doesn't make sense.

    You need to use client-side scripts to do this.

    Remember, the asp.net server sends html code for the browser to interpret so anything that isn't possible in HTML has to be done with a script.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width