Link to home
Start Free TrialLog in
Avatar of DanielBorson
DanielBorsonFlag for United States of America

asked on

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?

Avatar of sabeesh
sabeesh
Flag of United States of America image

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

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of traxion
traxion

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of traxion
traxion

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