Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > focusing on a textbox

Reply
Thread Tools

focusing on a textbox

 
 
Steven Zilberman
Guest
Posts: n/a
 
      07-17-2003
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.
 
Reply With Quote
 
 
 
 
Steven
Guest
Posts: n/a
 
      07-17-2003
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.
>>.
>>

>.
>

 
Reply With Quote
 
 
 
 
Vidar Petursson
Guest
Posts: n/a
 
      07-17-2003
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" <(E-Mail Removed)> wrote in message
news:09a601c34c68$2688dfc0$(E-Mail Removed)...
> 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.
> >>.
> >>

> >.
> >



 
Reply With Quote
 
Chris R. Timmons
Guest
Posts: n/a
 
      07-17-2003
"Steven" <(E-Mail Removed)> wrote in
news:09a601c34c68$2688dfc0$(E-Mail Removed):

> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Focusing a JPanel? Ryan Stewart Java 0 12-04-2003 05:53 PM
Re: Canon 10D Focusing problem?? Bill Karoly Digital Photography 3 08-02-2003 01:19 PM
Strange 10D focusing prob upon startup Duke Digital Photography 1 07-24-2003 08:17 PM
Canon Powershot S400 focusing problems louise Digital Photography 3 07-24-2003 12:06 PM
Kodak DX6340 focusing Jim McQuillan Digital Photography 3 07-17-2003 04:53 PM



Advertisments