By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

Setting focus to another User Control

P: n/a
I have a page with several user controls on it.
Some of the UC's have multiple text boxes that get auto-populated via
the OnTextChanged event. When this happens, I lose my focus (where the
blinking cursor is).

I have some SetFocus JavaScript that works from a page event, but I'm
not sure what I'd need to change to make it work from within a UC and
have it set focus to another control inside another UC. To complicate
this further, I'd like to be able to make the destination control that
receives focus variable, so that if the controls are re-ordered, the
next one down gets focus regardless of what is is.

Can this be done? Below is the JavaScript that works from a page event,
but I'm having trouble with it because I'm not even sure if I can find
my other controls from within a UC to pass this a non-null control.

Public Shared Sub SetInitialFocus(ByVal ctrl As Control)
Dim s As String
s = "<SCRIPT LANGUAGE='JavaScript'> function SetInitialFocus(){
Dim p As Control = ctrl.Parent
While Not TypeOf p Is System.Web.UI.HtmlControls.HtmlForm
p = p.Parent
End While
s = s & (p.ClientID)
s = s & "['"
s = s & ctrl.UniqueID
s = s & "'].focus(); }"
s = s & "window.onload = SetInitialFocus; </SCRIPT>"
ctrl.Page.RegisterClientScriptBlock("InitialFocus" , s.ToString())
End Sub

So to use this from Page_Load, I do somehting like:
Dim txtAddress As TextBox =
If Not IsNothing(txtAddress) Then SetInitialFocus(txtAddress)

Hope this makes sense. Thanks!

Nov 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.