Setting focus to FormView's child controls...

Discussion in 'ASP .Net' started by Guest, Mar 31, 2006.

  1. Guest

    Guest Guest

    How do I set the focus to a control that is a child to a FormView? I've tried
    the obvious (below) and lots of variations but none seam to work!

    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
    Me.Load
    Me.FormView1.Row.FindControl("TextBox1").Focus()
    End Sub
     
    Guest, Mar 31, 2006
    #1
    1. Advertisements

  2. Dick,

    Focus won't work on a web page. You have to set focus with javascript on the
    web.

    Here's a subroutine from a Javacript Component I give away for free on my
    website:

    Public Sub SetFocus(ByVal page As System.Web.UI.Page, ByVal webControl As
    System.Web.UI.WebControls.WebControl)

    If Not Page.ClientScript.IsStartupScriptRegistered("TextBoxFocus") Then

    Dim StringBuilder As New System.Text.StringBuilder

    With StringBuilder

    ..Append("<script language=""javascript"">" & vbCrLf)

    ..Append(vbTab & "<!--" & vbCrLf)

    ..Append(vbTab & vbTab & "document.getElementById('" & webControl.ClientID &
    "').focus();" & vbCrLf)

    ..Append(vbTab & "//-->" & vbCrLf)

    ..Append("</script>" & vbCrLf)

    End With

    page.ClientScript.RegisterStartupScript(GetType(String), "TextBoxFocus",
    StringBuilder.ToString)

    End If

    End Sub

    This one is converted for .net 2.0 currently the component on the site is
    written for .net 1.1 so if you need that you can see a demo of all the
    scripts in the component and download the source code for free from here:

    http://www.aboutfortunate.com/default.aspx?page=javascriptdemo

    Regards,

    --
    S. Justin Gengo
    Web Developer / Programmer

    Free code library:
    http://www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
     
    S. Justin Gengo, Mar 31, 2006
    #2
    1. Advertisements

  3. Try this:

    Dim TextBox1 As TextBox = (TextBox)FormView1.FindControl("TextBox1")
    TextBox1.Focus()
     
    Christopher Reed, Apr 2, 2006
    #3
  4. Hi Richard,

    If you're going to reference any sub controls in the FormView, make sure
    the ID is correct and the Template (where the control reside ) is the
    current active template( e.g if the FormView is in edit mode, we can only
    reference controls in the edit template...).

    Also, for setting focus on a certain control, we can also use the ASP.NET
    2.0 page class's "SetFocus" method(Page.SetFocus) to set the page's focus
    on a certain control. We can pass the control's reference into the funciton.

    Please have a try on it and let me know if there is still any problem.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Apr 3, 2006
    #4
  5. So you can use this: Page.SetFocus(FormView1.FindControl("Textbox1"))
     
    Christopher Reed, Apr 3, 2006
    #5
  6. Guest

    Guest Guest

    Cool. That worked just fine. Thanks.
     
    Guest, Apr 4, 2006
    #6
  7. Guest

    Guest Guest

    Page.SetFocus(FormView1.FindControl("Textbox1")) was just what I needed.
    Thanks.
     
    Guest, Apr 4, 2006
    #7
  8. You're welcome Richard,

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Apr 5, 2006
    #8
  9. Guest

    Ben Guest

    Is there any way to do this for a textbox inside a datalist?
    (So on Edit Template, the page should set focus on the textbox). Also if
    the list spans more than the screen length, editing something below
    should retain focus on the bottom and not reload page with the focus on
    top.
    Thanks in Advance,
    Arthur
     
    Ben, Apr 13, 2006
    #9
  10. Guest

    Ben Guest

    Is there any way to do this for a textbox inside a datalist?
    (So on Edit Template, the page should set focus on the textbox). Also if
    the list spans more than the screen length, editing something below
    should retain focus on the bottom and not reload page with the focus on
    top.
    Thanks in Advance,
    Arthur
     
    Ben, Apr 13, 2006
    #10
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.