473,756 Members | 3,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set focus to a textbox on page load

I am new to ASP/ASP.NET so kindly be gentle. When my index.aspx page
loads, I need the focus to be on one of the textboxes called
txtUserName.

I come from a VB background, so expecting the obvious, I go to the
Page_Load event of the index.aspx page and try to write
txtUserName.Set Focus but I see there isn't a SetFocus method for the
System.Web.UI.W ebControls.Text Box class.

What's the way to set the focus to a textbox on the page load in
ASP.NET?

Nov 19 '05 #1
3 6473
It's a client-side task. You need to call javascript focus() method on the
element you want to set focus on.

Eliyahu

"Water Cooler v2" <wt*****@yahoo. com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
I am new to ASP/ASP.NET so kindly be gentle. When my index.aspx page
loads, I need the focus to be on one of the textboxes called
txtUserName.

I come from a VB background, so expecting the obvious, I go to the
Page_Load event of the index.aspx page and try to write
txtUserName.Set Focus but I see there isn't a SetFocus method for the
System.Web.UI.W ebControls.Text Box class.

What's the way to set the focus to a textbox on the page load in
ASP.NET?

Nov 19 '05 #2
you can write a function something like this and use it when u need them.

protected void SetFocus(Contro l controlToFocus) {
string formName = GetFormName(con trolToFocus);
string jsString="<scri pt language=javasc ript>document." + formName +
".elements['" + controlToFocus. UniqueID + "'].focus();</script>";
if(Page.IsStart upScriptRegiste red("SetFocusTo Search")==false )
Page.RegisterSt artupScript("Se tFocusToSearch" ,jsString);

Hope this helps.
--
Kannan.V
Home : http://www.kannanv.com
Blog : http://kannanv.blogspot.com
Web : http://www.DotnetLounge.net

"Any one who has never made a mistake has never tried anything new" - Einstein
"Water Cooler v2" wrote:
I am new to ASP/ASP.NET so kindly be gentle. When my index.aspx page
loads, I need the focus to be on one of the textboxes called
txtUserName.

I come from a VB background, so expecting the obvious, I go to the
Page_Load event of the index.aspx page and try to write
txtUserName.Set Focus but I see there isn't a SetFocus method for the
System.Web.UI.W ebControls.Text Box class.

What's the way to set the focus to a textbox on the page load in
ASP.NET?

Nov 19 '05 #3
As the last post adviced this should do the trick:-

Private Sub SetFocus(ByVal ctrl As Control)
' Define the JavaScript function for the specified control.
Dim focusScript As String = "<script language='javas cript'>" & _
"document.getEl ementById('" + ctrl.ClientID & _
"').focus() ;</script>"

' Add the JavaScript code to the page.
Page.RegisterSt artupScript("Fo cusScript", focusScript)
End Sub

Hope this helps
Patrick

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #4

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

Similar topics

1
12450
by: Robert Scheer | last post by:
Hi. I have a function to submit the form when my user types any character on a textbox. I call the function on the keyup event of the textbox. The function submits the form and set the focus on the textbox, after the submit. The form submits ok, but after the submit the focus won't return to the textbox. Can anyone help me to solve that problem? That's the function: <Script language='Javascript'>
1
9826
by: Olav Tollefsen | last post by:
I use this code to set focus to a textbox when I load a page: private void SetFocus(System.Web.UI.Control ctrl) { string s = "<SCRIPT language=\"javascript\">document.getElementById('" + ctrl.ID + "').focus()</SCRIPT>"; RegisterStartupScript("focus", s); }
10
1700
by: tshad | last post by:
I have a page where I give focus to the first textbox on my page: <body onLoad="document.forms.firstName.focus();"> I also go and check the status of some data when I exit the 3rd textbox on my screen. The problem is that the screen jumps back to the first textbox because of the onLoad statement when the page is posted back. Is there an easy way to tell it to do the Onload only once and to go the next textbox on the repost?
2
2232
by: Water Cooler v2 | last post by:
I am new to ASP/ASP.NET so kindly be gentle. When my index.aspx page loads, I need the focus to be on one of the textboxes called txtUserName. I come from a VB background, so expecting the obvious, I go to the Page_Load event of the index.aspx page and try to write txtUserName.SetFocus but I see there isn't a SetFocus method for the System.Web.UI.WebControls.TextBox class. What's the way to set the focus to a textbox on the page load...
6
1435
by: nil | last post by:
hi..suppose there is only one textbox in the form and one command button... can anyone tell me how can i setfocus of textbox when page is load? should i do that by java script?please send me coding how to do that? and can anyone tell me how can i send mail to the group of user?and how can i add user in the vb.net? i am developing one web application for one designing institute and they want mailing facility so if i send one mail then...
5
2040
by: Finn Stampe Mikkelsen | last post by:
Hi How can i set a focus to a textbox in my codebehind page?? I have this WebForm, that takes information from a user and 2 buttons on the form. One that takes action on the entered information and one that resets the form... The second on, the reset button, i would like to set focus back to the first textbox on the form. I already have an onload in my body, which sets the
24
3921
by: erickme | last post by:
Well I wasn't to sure if post this on the javascript forum or here, but since is an error that probably I got because something related to my ASP.Net code I decided to posted here. Ok my problem is that I need to set focus on a textbox everytime the page is loaded. Maybe you already thinking what I thought at first, that with a textbox.setFocus() that will do it. Nop this is .Net Framework 1.1 and textbox.setFocus() didn't exist back then....
8
6904
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" key how can I set the focus to the next box after the postback? Please help! Using Visual Studio 2005 Pro, Asp.net 2.0, vb.net, WinXP
11
3929
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" AutoEventWireup="false" Codebehind="Login.aspx.vb" Inherits="Forsyth.HR_ReportingTool.UI.Login" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD>
0
9273
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
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...
1
9841
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9711
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7244
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
6534
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
5141
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
5303
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
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 we have to send another system

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.