unable to set focus to control after validation (must tab twice to exit control)

Discussion in 'ASP .Net Web Controls' started by Mad Scientist Jr, Jul 13, 2004.

  1. i am trying to set focus to a specific control depending on the
    outcome of a validator control and it is not working.

    none of these methods are working to setfocus:

    1. RegisterStartupScript("sf","<scriptlanguage='javascript'>document.form.MyControlID.focus();</script>");

    2. Andy Smith's FirstFocus control from
    http://www.metabuilders.com/tools/FirstFocus.aspx

    on my onchange event of field #1, based on the isvalid property of the
    field #1 validators, i want to set focus either back to field #1 if it
    is invalid, or to the next field #2 if it is valid. either way, it
    always sets focus back to field #1 no matter what, so the user has to
    press Tab twice to get to the next control, which is inconsistent with
    all the other controls on the screen:

    note the field names are generic Text1 and Text2 for simplicity

    any help would be appreciated

    Private Sub Text1_TextChanged(ByVal sender As System.Object, ByVal
    e As System.EventArgs) Handles Text1.TextChanged

    'run validation event
    RequiredFieldValidator_Text1.Validate()
    RegularExpressionValidator_Text1.Validate()

    If (RequiredFieldValidator_Text1.IsValid and
    RegularExpressionValidator_Text1.IsValid) then
    ' RECALCULATE SOME FIELDS
    Call ReCalculateFields()
    End If

    ' SET FOCUS ON NEXT CONTROL IF VALID, ELSE STAY IN SAME
    CONTROL

    Dim FirstFocus1 As MetaBuilders.WebControls.FirstFocus
    FirstFocus1 = New MetaBuilders.WebControls.FirstFocus
    FirstFocus1.Enabled = True

    If RequiredFieldValidator_Text1.IsValid And
    RegularExpressionValidator_Text1.IsValid Then ' goto next control

    'this didn't work:
    'Page.RegisterClientScriptBlock("SetFocus", "<script>
    language='javascript'>document.body.onload = function(){
    document.form.Text2.focus();}</script>")

    'this didn't work either
    'RegisterStartupScript("SetFocus1",
    "<scriptlanguage='javascript'>document.form.Text2.focus();</script>")

    'this also doesn't work:
    FirstFocus1.ControlToFocus = Text2.UniqueID

    Else

    'this didn't work:
    'Page.RegisterClientScriptBlock("SetFocus", "<script>
    language='javascript'>document.body.onload = function(){
    document.form.Text1.focus();}</script>")

    'this didn't work either:
    'RegisterStartupScript("SetFocus1",
    "<scriptlanguage='javascript'>document.form.Text1.focus();</script>")

    'this also doesn't work:
    FirstFocus1.ControlToFocus = Text1.UniqueID

    End If
     
    Mad Scientist Jr, Jul 13, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mad Scientist Jr
    Replies:
    2
    Views:
    5,346
    Mad Scientist Jr
    Jun 29, 2004
  2. Manav
    Replies:
    0
    Views:
    808
    Manav
    Oct 15, 2003
  3. Replies:
    2
    Views:
    494
    Jeff Epler
    May 31, 2005
  4. Roger
    Replies:
    3
    Views:
    379
  5. Andrew Pollock
    Replies:
    1
    Views:
    122
    Captain Paralytic
    Sep 16, 2009
Loading...

Share This Page