HOW TO HIGHLIGHT TEXT WITHIN ASP CONTROL ON PAGE LOAD

P

parm

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
 
R

Rick Strahl [MVP]

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
 
P

parm

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top