473,503 Members | 12,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set focus to a control on Page_Load

Hi all,

Can anyone tell me how to set focus to particular control on Page_Load() in
ASP.Net? I could not find any Control.Focus() method.

Thanks for any help!

Tedmond

Feb 13 '06 #1
3 1468
Howdy,

-- BEGIN CODE --

public static void SetFocus(Control control)
{
System.Text.StringBuilder script = new System.Text.StringBuilder();

script.Append("\r\n<script language='JavaScript'>\r\n");
script.Append("<!--\r\n");
script.Append("function OnWindowLoad()\r\n");
script.Append("{\r\n");
script.Append("\tdocument.");

Control parent = control.Parent;

// get parent form id
while (!(parent is System.Web.UI.HtmlControls.HtmlForm))
parent = parent.Parent;

script.Append(parent.ClientID);
script.Append("['");
script.Append(control.UniqueID);
script.Append("'].focus();\r\n");
script.Append("}\r\n");
script.Append("window.onload = OnWindowLoad;\r\n");
script.Append("// -->\r\n");
script.Append("</script>");

control.Page.RegisterClientScriptBlock("SetFocusSc ript",
script.ToString());
}

-- END CODE --

--
Milosz Skalecki
MCP, MCAD
"Tedmond" wrote:
Hi all,

Can anyone tell me how to set focus to particular control on Page_Load() in
ASP.Net? I could not find any Control.Focus() method.

Thanks for any help!

Tedmond

Feb 13 '06 #2
If you're using 1.x, you have to build your own using Javascript. The Focus
method is available in 2.0.
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

"Tedmond" <Te*****@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
Hi all,

Can anyone tell me how to set focus to particular control on Page_Load()
in
ASP.Net? I could not find any Control.Focus() method.

Thanks for any help!

Tedmond

Feb 13 '06 #3
Hi Tedmond,

Set the TabIndex of the control (Which needs focus) to 0, and set all
the remaining controls TabIndex to consequent numbers.

This worked for me.

Cheers,
Kris

Feb 13 '06 #4

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

Similar topics

1
1663
by: Paul Tsai | last post by:
Dear All, I used csharp web form to develop my company web application, and my question is how shuld I do if I want to focus on my textbox(ex. like Java Script form.txtID.focus()). Thanks !!...
5
1351
by: Glenn T. Kitchen | last post by:
Hello All, Does anyone know how to set the focus to a textbox control on Page_Load? Thank you, Glenn
3
4611
by: Kurt Schroeder | last post by:
I'm setting focus depenting upon which field has changed value. I'm doing this with the following: Page.RegisterStartupScript("SetFocus", "<script language=""Jscript"" >...
0
1428
by: MattB | last post by:
I have a page with several user controls on it. Some of the UC's have multiple text boxes that get auto-populated via the OnTextChanged event. When this happens, I lose my focus (where the...
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
5124
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...
4
4031
by: Jason | last post by:
Hi, Here's the scenario: I have a web application that has window A and window B. A user has both window A and B open - window A is in the foreground and window B is behind it. If the...
8
6888
by: Mel | last post by:
I have several text boxes and drop-down lists in an AJAX Update Panel. All user inputs have the Postback property set to True. After I type something in the first input entry and press the "Tab"...
11
3909
by: slinky | last post by:
I'm trying to simply set the focus of "txtUserName" upon opening this login form. I put in some Javascript but it is not working. Any ideas? Thanks! <%@ Page Language="vb"...
0
7212
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
7296
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
7364
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
5604
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
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
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
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.