473,795 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Focus On TextBox

A.M
Hi,

Can I configure a TextBox to have focus and cursor on it when page gets
loaded?

Thanks,
Alan
Nov 18 '05 #1
3 1189
A couple of approaces to use

The easiest is traditional HTML.
<body onload="javascr ipt:document.my Formname.focusi temname.focus() ;">

where focusitemname is the textbox id

You could easily set this using registerstartup script and setting the
focusitem dynamically
--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP

"A.M" <no*****@online .nospam> wrote in message
news:un******** *****@TK2MSFTNG P10.phx.gbl...
Hi,

Can I configure a TextBox to have focus and cursor on it when page gets
loaded?

Thanks,
Alan

Nov 18 '05 #2
Sure - you can inject some javascript into the page that does this for you:

Page.RegisterSt artupScript("Fo cusedControl",
"<script language='javas cript'>\r\ndocu ment.getElement ById('" +
YourControl.Id + "').focus();\r\ n</script>\r\n");
Better yet create a custom page class (or add it to your customized Page
class) by adding a FocusedControl property and then handling the insertion
automatically via the OnPreRender() event handler:

/// <summary>
/// Assigns focus to the specified control. Note the name must match the
exact
/// ID or container Id of the control in question.
/// Logic for this behavior is provided in OnPreRender()
/// </summary>
[Category("Behav ior"),
Description("Se t the focus of this form when it starts to the specified
control ID")]
public Control FocusedControl
{
get { return this.oFocusedCo ntrol; }
set { this.oFocusedCo ntrol = value; }
}
Control oFocusedControl = null;
/// <summary>
/// Overriden to handle the FocusedControl property.
/// </summary>
/// <param name="e"></param>
protected override void OnPreRender(Eve ntArgs e)
{
if (this.FocusedCo ntrol != null)
this.RegisterSt artupScript("Fo cusedControl"," <script
language='javas cript'>\r\ndocu ment.getElement ById('" +
this.FocusedCon trol.ID + "').focus();\r\ n</script>\r\n");

base.OnPreRende r (e);
}
--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
----------------------------------
Making waves on the Web
"A.M" <no*****@online .nospam> wrote in message
news:un******** *****@TK2MSFTNG P10.phx.gbl...
Hi,

Can I configure a TextBox to have focus and cursor on it when page gets
loaded?

Thanks,
Alan

Nov 18 '05 #3
Check out this faq,
http://www.extremeexperts.com/Net/Co...lementAfterPos
tback.aspx

--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com

"A.M" <no*****@online .nospam> wrote in message
news:un******** *****@TK2MSFTNG P10.phx.gbl...
Hi,

Can I configure a TextBox to have focus and cursor on it when page gets
loaded?

Thanks,
Alan

Nov 18 '05 #4

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

Similar topics

17
3865
by: Neil Ginsberg | last post by:
OK, this is a stupid thing, but I can't seem to get this to work. I have a form with a subform (in continuous form view). A combo box on the main form has code in the AfterUpdate event which adds a record to the subform (based on the value of the combo box) and requeries the subform control. I want the focus to return to the combo box on the main form when it's done, but I can't get it to do so if the user enters a value and presses Enter...
0
5084
by: Miquel | last post by:
Hi all. I felt frustrated when developing an 'UserControl' derivated from textBox, because sequence event (and Validate event) seems to fail. I Always thought my code was wrong. But after trying the UserControl a lot of hours I have the feeling that c# (or .Net) fails. Thus, I have developed a simple program with 3 textBox, 1 Botton and 1 ListBox (for showing results) which fire OnEnter, OnGotFocus,
2
2018
by: Jesper | last post by:
Last week I posted this for the first time. However, I didn't get any useable solution to my problem. I'm very keen on getting this problem solved, so please excuse me for trying again. I'll try to be a little more precise. I have an app running (minimized) and without showing itself on the task bar. It puts an icon in the task pane (next to the clock), and when I click this icon, a message handler is invoked and creates a new usercontrol...
4
3951
by: SJ | last post by:
Hi all, I have come across a weird problem when attempting to automatically set the focus in a vb.net form to a checkbox control... In my form I have (on a tab page in a tab control) several textboxes and a checkbox. The behaviour I want from my app is as follows:- When the textbox (which is prior in tab order to the tab control) has been filled with a certain length of text by the user, the focus is
5
8731
by: Tosch | last post by:
I have a usercontrol with a label, a textbox, a treeview, a grid and a couple of checkboxes. The usercontrol is hosted on a form together with a cancel and a accept button. This form is used to search records in a database. On several forms in my application I show (modal) this form to search records. Everytime I show the form I want the focus to be on the textbox on the usercontrol. This works the first time I display the form. But...
3
2825
by: vivela | last post by:
hi, I hope you could help me out on this one,cause I have been trying for 4 days to solve it,but couldn't quite get it right: I have an ASP textbox that should change the value in a dropdownlist. this happens,but the problem is with the validation of the ddl.it has a requiredfieldvalidator,and the error message disapears only if i select a value from the dropdownlist,even though it should also disapear when I write a valid name in the...
3
6773
by: Henry Jones | last post by:
VS 2005 On a form I have a tabcontrol with 5 tabs. Each tab has three textboxes on it. When I click on each tab, I would like to have the focus set to the first textbox. On the tabenter event for each tab, I put a textbox1.focus but the textbox doesn't contain the cursor. How can I do this?
3
4925
by: hugo | last post by:
Hi, I have a function that I call from form fields using the OnKeyUp function to replace special caracters. Once this function has been called, it does not set the focus on the form field where I have called the function from. What happens : I laod a EDIT page with a html form on it and fill in the fields with
3
5198
by: jp2express | last post by:
I have several applications that use panels as screens, but I can *not* seem to set the focus for a Textbox. Panel1.BringToFront() Panel1_Textbox.Focus() ' do something with a control on Panel1 Panel2.BringToFront() Panel2_Textbox.Focus() ' do something with a control on Panel2
10
10049
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
10443
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9044
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.