HOW TO HIGHLIGHT TEXT WITHIN ASP CONTROL ON PAGE LOAD

Discussion in 'ASP .Net' started by parm, Oct 28, 2005.

  1. parm

    parm Guest

    How do I highlight text in my first (tabindex = 1) asp control (eg:
    textbox) on the webform on page load. If I try to set focus using
    javascript then I get script error "Cannot move focus to invisible
    control)". Because the tabindex of first control is 1 therfore I can
    get the focus on first control but the text inside is not highlighted.
    Any help would be greatly appriciated.

    My set focus javascript:
    Public Sub SetFocus(ByRef MyPage As System.Web.UI.Page, _
    ByVal objMyObject As Object)

    Dim ctrLiteralControl As LiteralControl

    On Error GoTo ErrorMsg

    ctrLiteralControl = _
    New LiteralControl("<script language=JavaScript>" &
    _
    "document.getElementById('" & objMyObject.ID &
    "').focus();" & _
    "document.getElementById('" & objMyObject.ID &
    "').select();" & _
    "</script>")
    MyPage.Controls.Add(ctrLiteralControl)
    End Sub
    parm, Oct 28, 2005
    #1
    1. Advertising

  2. I suspect you're not putting that code in the right place. You should put
    that code just before the </form> tag to run when the page loads, or call
    the function from the body onload event.

    If the error you're getting is accurate it sounds like your control is
    actually not visible in which case you can't set focus. Otherwise that
    should work - I use the same approach, although I've built it into my custom
    Page class.

    Select is IE specific I think... but since you're using VBScript I guess
    you're targeting IE anyway.

    +++ Rick ---

    --

    Rick Strahl
    West Wind Technologies
    www.west-wind.com
    www.west-wind.com/weblog

    "parm" <> wrote in message
    news:...
    > How do I highlight text in my first (tabindex = 1) asp control (eg:
    > textbox) on the webform on page load. If I try to set focus using
    > javascript then I get script error "Cannot move focus to invisible
    > control)". Because the tabindex of first control is 1 therfore I can
    > get the focus on first control but the text inside is not highlighted.
    > Any help would be greatly appriciated.
    >
    > My set focus javascript:
    > Public Sub SetFocus(ByRef MyPage As System.Web.UI.Page, _
    > ByVal objMyObject As Object)
    >
    > Dim ctrLiteralControl As LiteralControl
    >
    > On Error GoTo ErrorMsg
    >
    > ctrLiteralControl = _
    > New LiteralControl("<script language=JavaScript>" &
    > _
    > "document.getElementById('" & objMyObject.ID &
    > "').focus();" & _
    > "document.getElementById('" & objMyObject.ID &
    > "').select();" & _
    > "</script>")
    > MyPage.Controls.Add(ctrLiteralControl)
    > End Sub
    >
    Rick Strahl [MVP], Oct 28, 2005
    #2
    1. Advertising

  3. parm

    parm Guest

    Hi Rick,

    My javascript to set focus is within a vb.net function called
    SetFocus(). I am calling this function from vb.net page load event.
    Because the page is not loaded when the SetFocus() function is called
    therefore I am getting the above error message. I also call the same
    SetFoucs() function from other places in my code and it works fine. I
    just cannot get it to work on page load. I am calling some other
    function on body onload event + my javascript is embeded in vb.net
    function.
    parm, Oct 28, 2005
    #3
    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. Robert M
    Replies:
    1
    Views:
    665
    Curt_C [MVP]
    Jan 5, 2004
  2. Replies:
    1
    Views:
    508
    Adrienne Boswell
    Mar 10, 2006
  3. Michael SL

    How do you Highlight text within asp:textbox?

    Michael SL, Feb 13, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    139
    Michael SL
    Feb 13, 2004
  4. Replies:
    0
    Views:
    226
Loading...

Share This Page