[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 994
  • Last Modified:

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

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?

  • 2
1 Solution
Try to use wizard.FindControl("texboxname"); for get the text box and
set focus
DanielBorsonAuthor Commented:
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?
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)
My Javascript may be incorrect....it may need to be:
"<script language='javascript'>document.getElementById('" & strID & "').focus();</script>"

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now