Setting focus to another User Control

M

MattB

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(){
document."
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 =
FindControl("UC1").FindControl("TextBox1")
If Not IsNothing(txtAddress) Then SetInitialFocus(txtAddress)

Hope this makes sense. Thanks!

Matt
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top