Default control focus in ASP.NET 2.0 Wizard on page using Master template

Posted on 2007-07-20
Last Modified: 2008-01-09
In ASP.NET 2.0, I have a Wizard control that is inside of an <asp:Content> tag. The <form> tag is in a Master template file. My question is how can I set a textbox to have default focus? When I try to do it from the code-behind by using "textbox.Focus()" I get an error that there is no Form, and therefore the focus cannot be set. On other pages that also use the Master template, I employ client-side JavaScript to set focus, but since this is a Wizard control, there would be several controls that would need default focus at different steps in the Wizard.

I've thought of a workaround by having a hidden textobx with the Wizard ActiveStepIndex and a switch/case in the client-side JavaScript that would set the focus to different controls, based on the ActiveStepIndex, but that seems like a bigger workaround than should be necessary. Is there another (and simpler) way to do this?

Question by:DanielBorson
    LVL 8

    Expert Comment

    Try to use wizard.FindControl("texboxname"); for get the text box and
    set focus

    Author Comment

    Doesn't work. Here's the error I'm getting:

    "A form tag with runat=server must exist on the Page to use SetFocus() or the Focus property."

    The "FindControl" is finding the textbox just fine. I can see it in the watch when I do "Wizard.FindControl("textbox")". But in watch, "this.Form" is null. The form tag is in the Master, and it does have a runat="server" attribute.

    I'm not sure what's going on. Can you enlighten me?
    LVL 9

    Accepted Solution

    If FindControl is working:

    Dim objTB As TextBox = Wizard.FindControl("textbox")
    Dim strID As String = objTB.ClientID 'this will give you the id on the page

    Dim myScript As String = "<script language='javascript'>document.getElementById('" & strID & "').setFocus();</script>"

    Now you can inject some javascript with Page.RegisterStartupScript

    Page.RegisterStartupScript(Me.GetType(), "tbFocus", myScript)
    LVL 9

    Expert Comment

    My Javascript may be may need to be:
    "<script language='javascript'>document.getElementById('" & strID & "').focus();</script>"

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now