Solved

My content page has many user controls.  How do I set the focus to a specific usercontrol?

Posted on 2007-11-18
6
250 Views
Last Modified: 2011-10-03
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
0
Comment
Question by:katzhoward
  • 3
  • 2
6 Comments
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20309005
You can't set the focus to a user control. A user control is something that only exists on the server while the page is created. When the pave reaches the browser, all that is left is the html code that the controls created.

You have to set focus to one of the html elements in the page.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20309522
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:


<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>

Open in new window

0
 

Author Comment

by:katzhoward
ID: 20312332
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20314613
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.RegisterStartupScript(Page.GetType(), "Focus", "document.getElementById('" + D1009.FocusControl.ClientID + "').focus();", True)
0
 

Author Comment

by:katzhoward
ID: 20315090
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.RegisterStartupScript 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.RegisterStartupScript(Page.GetType(), "FocusControl", "document.getElementById('D1009.FocusControl.ClientID').focus();", True)

but when I run it I get
document.getElementById(...) is nul or not an object

any ideas?

0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 20315338
Put the code in the page instead of the master page, and it works.

If you want it in the master page, you have to cast the Page reference to the specific class of the page to access it's members:

Page.ClientScript.RegisterStartupScript(Page.GetType(), "Focus", "document.getElementById('" + DirectCast(Page, ActualClassOfThePage).D1009.FocusControl.ClientID + "').focus();", True)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 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

25 Experts available now in Live!

Get 1:1 Help Now