Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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.

 

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...