By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,728 Members | 2,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,728 IT Pros & Developers. It's quick & easy.

Focus on the first control

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.