473,321 Members | 1,708 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,321 software developers and data experts.

Javascript to Set Focus to TextBox

I have a javascript piece that sets focus to a textbox on one of my
webforms. The script works fine for IE: it sets the cursor blinking in
the textbox. In Firefox, however, it doesn't work at all.

Below are two snippets from the page source as pulled directly out of
Firefox after viewing the page:
I. HTML CODE BLOCK FOR TEXTBOX

<td align="right" width="100">
<input name="ctl00$txtUserName" type="text" maxlength="30"
id="ctl00_txtUserName"
style="border-width:0px;border-style:None;width:100px;" />
</td>

II. SCRIPT CODE BLOCK

<script>var
txtBox=document.getElementById("ctl00_txtUserName" );if(txtBox!=null)document.all.ctl00_txtUserName.f ocus();</script>
There is also another script block immediately following this one.

I have used this code on a couple of other sites, and it has always
worked for both browsers. Now, I am having a problem for some reason.
The only thing new here is that I am now using VS2005 and
ClientScript.RegisterStartupScript instead of VS2003 and
Page.RegisterStartupScript.

Any ideas?

Dec 23 '05 #1
3 12712
i don't know why the script fails on Firefox browsers, but, if you're
only concerned with setting the focus, you might want to try the
ASP.NET 2.0 control "Focus" method :

protected void Page_Load(object sender, EventArgs e)
{
this.TextBox2.Focus();
}

hth

seasons greetings,
albert

Dec 23 '05 #2
firefox, uses the w3c dom, not the IE proprietary dom (.all collection).
change code to:

<script>
var txtBox=document.getElementById("ctl00_txtUserName" );
if (txtBox!=null ) txtBox.focus();
</script>
-- bruce (sqlwork.com)
"Joey" <jo*********@topscene.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I have a javascript piece that sets focus to a textbox on one of my
webforms. The script works fine for IE: it sets the cursor blinking in
the textbox. In Firefox, however, it doesn't work at all.

Below are two snippets from the page source as pulled directly out of
Firefox after viewing the page:
I. HTML CODE BLOCK FOR TEXTBOX

<td align="right" width="100">
<input name="ctl00$txtUserName" type="text" maxlength="30"
id="ctl00_txtUserName"
style="border-width:0px;border-style:None;width:100px;" />
</td>

II. SCRIPT CODE BLOCK

<script>var
txtBox=document.getElementById("ctl00_txtUserName" );if(txtBox!=null)document.all.ctl00_txtUserName.f ocus();</script>
There is also another script block immediately following this one.

I have used this code on a couple of other sites, and it has always
worked for both browsers. Now, I am having a problem for some reason.
The only thing new here is that I am now using VS2005 and
ClientScript.RegisterStartupScript instead of VS2003 and
Page.RegisterStartupScript.

Any ideas?

Dec 23 '05 #3
How to select all text in this case?
The tb does not hi-lite it's contents this way.
I might need to resort to js then.. to bad..

"albert braun" <bc***********************@yahoo.com> schreef in bericht
news:11**********************@z14g2000cwz.googlegr oups.com...
i don't know why the script fails on Firefox browsers, but, if you're
only concerned with setting the focus, you might want to try the
ASP.NET 2.0 control "Focus" method :

protected void Page_Load(object sender, EventArgs e)
{
this.TextBox2.Focus();
}

hth

seasons greetings,
albert

Dec 27 '05 #4

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

Similar topics

14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
0
by: ED M. | last post by:
Hello all...I'm new to the board. I have a problem that I hope someone here might be able to solve for me. I am doing some clientside validation using Javascript. The text I am testing is...
0
by: Michelle Keys | last post by:
I am trying to call a print function to print a string from a database using javascript. Which is RC_DATA of Varchar2(2500). This is a javascript is not being used. I have a thing that needs to...
0
by: Mad Scientist Jr | last post by:
I have a textbox that i am adding to (in codebehind of ASP.NET) and need to ensure that the focus is scrolled to the bottom of the textbox each time the page refreshes, and then set focus to a 2nd...
1
by: Roshawn Dawson | last post by:
Hi, Could someone tell how to force a mouse event using either jscript or javascript? I simply want to allow users to scroll the page after postbacks using the scroll button on either a mouse...
7
by: joey.powell | last post by:
I have a home page with username and password textboxes and a login button for purposes of users being able to log in (forms authentication) directly on the site home page. I also have a dedicated...
2
by: silpa | last post by:
Hi, I have placed two text boxes(server side) for Name and Age in asp.net 2.0. Their ids are txtPatientName and txtPatientAge. The validation to be done is both text boxes should not be...
11
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
by: =?Utf-8?B?QWJoaQ==?= | last post by:
Hi All, i have 3 textboxes for area Code,phone number and Extention and each have MaskedEditExtender associated with it. i want that when the length of first text box reaches it's maxlength ,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.