Howard Katz
asked on
My content page has many user controls. How do I set the focus to a specific usercontrol?
Hello Experts,
I have an application that has 1 Master page and 7 content pages. All the controls on my content pages are user controls. How do I set the focus to a specific user control.
Thank you
Howard
I have an application that has 1 Master page and 7 content pages. All the controls on my content pages are user controls. How do I set the focus to a specific user control.
Thank you
Howard
On the master page you can check wether the specific control is loaded or not and if loaded set the focus to that control.
But I would do set the focus in the apropriate content page. You need not to change the onLoad eent of the master page for that. Simply add a script line below the control and you are done.
Like this:
But I would do set the focus in the apropriate content page. You need not to change the onLoad eent of the master page for that. Simply add a script line below the control and you are done.
Like this:
<form name="myForm" >
<input type="text" name="myField" id="myField" >
<script>document.myForm.myField.focus()</script>
<!-- or if you prefer id -->
<script>document.getElementById("myField").focus()</script>
ASKER
Zvonko, It is a user control I am trying to set the focus on. The Id of the user control has an ID of D1009. Using the script method you suggest gives an error as the page loads. Any other ideas
As I said, you can't set focus to a user control. You have to set focus to a control that is rendered as an html element on the page.
Let's say that the user control in question contains a TextBox control with the id TextBox1.
Put a property in the user control so that the page can reach the text box:
Public ReadOnly Property FocusControl() As TextBox
Get
Return TextBox1
End Get
End Property
In the page you add a Javascript to set focus to the control:
Page.ClientScript.Register StartupScr ipt(Page.G etType(), "Focus", "document.getElementById(' " + D1009.FocusControl.ClientI D + "').focus();", True)
Let's say that the user control in question contains a TextBox control with the id TextBox1.
Put a property in the user control so that the page can reach the text box:
Public ReadOnly Property FocusControl() As TextBox
Get
Return TextBox1
End Get
End Property
In the page you add a Javascript to set focus to the control:
Page.ClientScript.Register
ASKER
GreenGhost,
I think this is close and thank you for your help. The user control does have a text box and I set up the property.
The user control is on a content page. I am putting the Page.client.RegisterStartu pScript in the Page_PreRender of the master page. It does not compile and I get the error:
: Name 'D1009' is not declard.
If i put it in like this it does compile
Page.ClientScript.Register StartupScr ipt(Page.G etType(), "FocusControl", "document.getElementById(' D1009.Focu sControl.C lientID'). focus();", True)
but when I run it I get
document.getElementById(.. .) is nul or not an object
any ideas?
I think this is close and thank you for your help. The user control does have a text box and I set up the property.
The user control is on a content page. I am putting the Page.client.RegisterStartu
: Name 'D1009' is not declard.
If i put it in like this it does compile
Page.ClientScript.Register
but when I run it I get
document.getElementById(..
any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You have to set focus to one of the html elements in the page.