473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

focusing on a textbox

Is there a way to focus on a textbox in ASP.Net?
Can I do something like textBoxMyTextBox.Focus() ???
Meaning, I can't do it with the JavaScript way, because
JavaScript does not know the form name, and for some
reason it does not see the id of the textbox. Thank you.
Nov 17 '05 #1
3 2925
For some reason it's not working. My asp code looks like
this:
<form runat="server">
<asp:TextBox id="UserName" runat="server" MaxLength="10"
Width="120px"></asp:TextBox>
</form>

The JavaScript code looks like this:
<script language="JavaScript">
<!--
window.document.all("UserName").focus();
//-->
</script>

I am getting a javascript error which says that
window.document.all(...) is a null or not an object.

Thanks.

Steven
-----Original Message-----
Hi,

I don't believe there is a way to do this with server- sideprogramming. However, doesn't the JavaScript
window.document.all("textBoxMyTextBox").focus() ; work?

Greetz,
Domien
-----Original Message-----
Is there a way to focus on a textbox in ASP.Net?
Can I do something like textBoxMyTextBox.Focus() ???
Meaning, I can't do it with the JavaScript way, because
JavaScript does not know the form name, and for some
reason it does not see the id of the textbox. Thank you..

.

Nov 17 '05 #2
Hi

Probably the element is not ready/loaded on the client when you call
focus....
Try changing the script to this... should be easy to modify to focus on any
element...

<script language="JavaScript">
<!--
function doFocus(){
if(document.forms[0].UserName) document.forms[0].UserName.focus();
}
onload = doFocus;
//-->
</script>

--
Best Regards
Vidar Petursson
==============================
Microsoft Internet Client & Controls MVP
==============================
"Steven" <st*************@alum.rpi.edu> wrote in message
news:09****************************@phx.gbl...
For some reason it's not working. My asp code looks like
this:
<form runat="server">
<asp:TextBox id="UserName" runat="server" MaxLength="10"
Width="120px"></asp:TextBox>
</form>

The JavaScript code looks like this:
<script language="JavaScript">
<!--
window.document.all("UserName").focus();
//-->
</script>

I am getting a javascript error which says that
window.document.all(...) is a null or not an object.

Thanks.

Steven
-----Original Message-----
Hi,

I don't believe there is a way to do this with server-

side
programming. However, doesn't the JavaScript
window.document.all("textBoxMyTextBox").focus() ; work?

Greetz,
Domien
-----Original Message-----
Is there a way to focus on a textbox in ASP.Net?
Can I do something like textBoxMyTextBox.Focus() ???
Meaning, I can't do it with the JavaScript way, because
JavaScript does not know the form name, and for some
reason it does not see the id of the textbox. Thank you..

.

Nov 17 '05 #3
"Steven" <st*************@alum.rpi.edu> wrote in
news:09****************************@phx.gbl:
For some reason it's not working. My asp code looks like
this:
<form runat="server">
<asp:TextBox id="UserName" runat="server" MaxLength="10"
Width="120px"></asp:TextBox>
</form>

The JavaScript code looks like this:
<script language="JavaScript">
<!--
window.document.all("UserName").focus();
//-->
</script>

I am getting a javascript error which says that
window.document.all(...) is a null or not an object.


Steven,

Look at the browser's HTML source for your page. I'll bet ASP.NET
modified the control name to something like _ctl0_UserName. ASP.NET
usually does this to controls in a UserControl (.ascx), so there
won't be a name collision if multiple instances of the same
UserControl are on the same page.

The way to fix this is to insert the JavaScript using server-side
code, and use the control's ClientId property to refer to the control
in the JavaScript code.

For example, you can put code like this in the Page_Load event of
your form (C#) to make the UserName TextBox control get focus:

string script = @"
<script language=""javascript"">
<!--
document.getElementById('{0}').focus();
//-->
</script>
";

if (!this.IsStartupScriptRegistered("SetWebControlFoc us"))
this.RegisterStartupScript("SetWebControlFocus",
string.Format(script, UserName.ClientID));

Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 17 '05 #4

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

Similar topics

3
by: LEE | last post by:
I used an OpenFileDialog control, it was not focused every time it was called. How to focus it? Thanks
2
by: Energy_Abs0rber` | last post by:
how can i make a focus on a textbox of my app? ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World!...
3
by: harig | last post by:
hi all.. how do i focus a textbox from a javascript i'e) <input type="text" size='50' name="txt1" maxlength='254' value="" onblur="checkDir()"> <input type="text" size='50'...
3
NawazAhmed
by: NawazAhmed | last post by:
Hi, I am working with Visual Studio 2003, framework 1.1 I am trying to focus a textbox when page loads. I tried Page.RegisterStartupScript("focus",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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...
0
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,...

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.