Focus on a text box in user contol, sitting on aspx page

taraf21

Newcomer
Joined
Mar 9, 2005
Messages
2
Hi, I hope someone can help me. The solution is probably very simple, but I'm not very good with Javascript and need some help with this problem...
I have a user control LoginTable that has my username and password fields on it. This user control sits on a web page, Login. (It's set up this way because the viewstate toggles for the logintable.) Anyway, all I'm trying to do is set the focus on the username textbox. I have a user control base class for all my control methods. I've tried putting the below code there and calling it from my user control, but it's not working. If I quick watch the control, it shows the textbox name, but when the aspx page loads, it doesn't set the focus. I also tried putting a call on my aspx page, but it just returns a Null Ref error.
I know I'm probably not explaining this incorrectly, but if anyone can help, I would REALLY appreciate it!!
Here's the code in my UserControlBase Class:

Code:
Public Sub FocusOnControl(ByVal ctrl As Control)
        ' Define the JavaScript function for the specified control.
        Dim focusScript As String = "<script language='javascript'>" & _
          "document.getElementById('" + ctrl.ClientID & _
          "').focus();</script>"

        ' Add the JavaScript code to the page.
        Page.RegisterStartupScript("FocusScript", focusScript)
End Sub

Here's my call in my user control.

Code:
Private Overloads Sub SetupControl(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here  
        Dim UserName As Control = txtUsernamePage
        SetVisibility()
        FocusOnControl(UserName)

    End Sub

Thanks again!
Tara
 
Back
Top