473,508 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Focus on the first control

Hi,

How can I set the focus on the first control in the page? I tried
Page.Controls(0).Focus() but it didn't works. Does anyone knows it?

Mar 10 '07 #1
4 1952

this sets focus to the field named 'ctlMyField'

ctlMyField.Page.RegisterClientScriptBlock("Initial Focus", "<SCRIPT
FOR='window' EVENT='onload' LANGUAGE='JScript'>document.all." + ctlMyField +
".focus();</SCRIPT>");
"Rafael A. M. Borges" <rb*******@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
Hi,

How can I set the focus on the first control in the page? I tried
Page.Controls(0).Focus() but it didn't works. Does anyone knows it?

Mar 11 '07 #2
On Mar 10, 11:02 pm, "xzzy" <mrbikej...@comcast.netwrote:
this sets focus to the field named 'ctlMyField'

ctlMyField.Page.RegisterClientScriptBlock("Initial Focus", "<SCRIPT
FOR='window' EVENT='onload' LANGUAGE='JScript'>document.all." + ctlMyField +
".focus();</SCRIPT>");

"Rafael A. M. Borges" <rborge...@gmail.comwrote in messagenews:11**********************@h3g2000cwc.go oglegroups.com...
Hi,
How can I set the focus on the first control in the page? I tried
Page.Controls(0).Focus() but it didn't works. Does anyone knows it?
Ok, but I want to do this in a master page, to set the focus on the
first control in the pages that inherits it. So I need to discover
programatically the first control of the page.

Thanks for your help

Mar 11 '07 #3
It could be tricky if you know nothing about the controls on content pages.
You could try polling through the controls to find the one with minimum
value of the TabIndex property and call SetFocus on it. But you can't
guarantee the right order of TabIndex values. Polling the Controls
collection should be recursive. And what will you do if the first control is
just an html control and not a web control in the first place?

I would suggest introducing a property on the master page where content page
would set the id of the control that should get focus and the master page
would set focus based on this id.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Rafael A. M. Borges" <rb*******@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
On Mar 10, 11:02 pm, "xzzy" <mrbikej...@comcast.netwrote:
>this sets focus to the field named 'ctlMyField'

ctlMyField.Page.RegisterClientScriptBlock("Initia lFocus", "<SCRIPT
FOR='window' EVENT='onload' LANGUAGE='JScript'>document.all." +
ctlMyField +
".focus();</SCRIPT>");

"Rafael A. M. Borges" <rborge...@gmail.comwrote in
messagenews:11**********************@h3g2000cwc.g ooglegroups.com...
Hi,
How can I set the focus on the first control in the page? I tried
Page.Controls(0).Focus() but it didn't works. Does anyone knows it?

Ok, but I want to do this in a master page, to set the focus on the
first control in the pages that inherits it. So I need to discover
programatically the first control of the page.

Thanks for your help

Mar 11 '07 #4
On Mar 11, 11:07 am, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.orgwrote:
It could be tricky if you know nothing about the controls on content pages.
You could try polling through the controls to find the one with minimum
value of the TabIndex property and call SetFocus on it. But you can't
guarantee the right order of TabIndex values. Polling the Controls
collection should be recursive. And what will you do if the first control is
just an html control and not a web control in the first place?

I would suggest introducing a property on the master page where content page
would set the id of the control that should get focus and the master page
would set focus based on this id.

--
An alternative is to define an interface that content pages must
implement to benefit from the focus treatment - e.g

public interface FocusFirst
{
WebControl First {get; set;}
}

the master page then addresses the interface like this

FocusFirst f = Page as FocusFirst;
if (f != null)
f.First.Focus();
Mar 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2204
by: Nitin | last post by:
Hi I have created function to check date and time. at the time of execution, if date is left empty the function returns the error message but then the focus goes to next field. Next filed is for...
4
2852
by: Colleyville Alan | last post by:
I have 2 command buttons on a form. I want the first one to be enabled and the second one to be disabled when the form is opened. That works fine. Then, after the user has pressed the first...
0
1534
by: LCAdeveloper | last post by:
Another newbie question I'm afraid. When I use the .Focus() or .Select() methods to set the initial control that has focus on a form, try as I might I cannot get the control to visually indicate...
1
2030
by: clickon | last post by:
For testing purposes i have got a 2 step WizardControl. Eqach step contains a text box, TextBox1 and TextBox2 respectively. If i put the following code in the respective activate event handlers...
1
5125
by: clickon | last post by:
For testing purposes i have got a 2 step WizardControl. Eqach step contains a text box, TextBox1 and TextBox2 respectively. If i put the following code in the respective activate event handlers for...
2
2462
by: Jonathan N. Little | last post by:
As part of a JavaScript precheck form validation I noticed a problem with trying to return focus to the field with an error. I have setup a demo page. ...
2
7163
by: Joergen Bech | last post by:
Hope someone has a solution or some suggestions for this. This cannot be right?!? Problem: I have multiple non-modal forms open at the same time. One or more of these forms have a...
3
2426
by: zacks | last post by:
I am working on an application that has a UI that supports multiuple functions by means of a group of "plug ins". Each plugin is a class library. Each plugin contains a User Control that defines...
10
9988
by: Derek Hart | last post by:
I can set focus to my property grid by using propgrid.Focus - but how can I set the default property that is always first? Or just set focus to the default property? Can this be done?
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.