Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set focus on label using Page.SetFocus 2.0

Status
Not open for further replies.

columbo2

Technical User
Jul 14, 2006
97
GB
Hi All,

Does anyone know how to use Page.SetFocus to set focus to a particular point on a page.
I have an error message that pops and I want the page to focus on it.

If I use Page.SetFocus I can set focus to a textbox but not to an anchor or label.

Can anyone help?

Thanks
 
In the future please use forum855 and forum216 for questions related to web development.

use the registerstartupscript method to append javascript to the page. the javascript will set the focus to your desired control. google javascript setfocus if you need the js code.

Tip: use myControl.ClientID to construct the js. not MyControl.ID. MyControlID is unique on the server, not the client. MyControl.ClientID will create a unique id on the client which will be required to find the control.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi, thanks for your help!

I've tried...

ClientScript.RegisterStartupScript(typeof(Page), "SetFocus", "<script>document.getElementById('" + labErrorText.ClientID + "').focus();</script>");

where labErrorText is the control I want to focus on.
Sadly, it doesn't work. I substituted in a simple alert and that works OK but the focus javascript doesn't work.

Can anyone see where I'm going wrong

thanks
C
 
focus is meant for input controls. if labErrorText is a Label you can't set the focus (99% s ure). instead you need to use a anchor tag.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi,

I put <a id="erroranc" runat="server"> .... </a> on the HTML page and then...

ClientScript.RegisterStartupScript(typeof(Page), "SetFocus", "<script>document.getElementById('" + erroranc.ClientID + "').focus();</script>");

but still no joy?

I've even tried a readonly textbox with no border and 1 width but it doesn't like that either.

Thanks
C

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top