Solved

SetFocus to Textbox on Client-Side in ASP.NET using VBScript

Posted on 2004-10-14
10
705 Views
Last Modified: 2012-06-21
I have a situation where I am doing something on the client-side in an ASP.NET application. Now, I want to set focus back to a particular textbox.

I think the code should be something like -

controlid = document.getElementById("txtCustomerName")
controlid.setfocus

But, of course, it doesn't work.

I get the Error: Object required. 'controlid'

or, when I've tried it with other controls on the same page I get -

Error: Object doesn't support this property or method: 'controlid.setfocus'

This should be doable. Any ideas?

 






0
Comment
Question by:gbmcneil
  • 5
  • 5
10 Comments
 
LVL 15

Accepted Solution

by:
praneetha earned 125 total points
ID: 12309711
document.all("txtCustomerName").focus();
0
 

Author Comment

by:gbmcneil
ID: 12310032
No need to continue.

praneetha has won the day.

How, how do I go about rewarding you the points?

Thanks very much.
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12310049
you would see accept answer next to my answer so u just click it and select the grade...

thank you.

good luck
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:gbmcneil
ID: 12310826
praneetha -

How about the same question in the Page_Load sub at launch.

Can I change the default SetFocus to a control other than the first one in the Z-Order?

The code: document.all("txtCustomerName").focus();

doesn't work on the server-side. When the page is loading, the system doesn't know what "document" is.

It triggers my Script Debugger and says:

Error: Name 'document' is not declared.

More thoughts?






0
 
LVL 15

Expert Comment

by:praneetha
ID: 12311047
hi

ok sorry for the delay add this code at the close of </html> tag

</HTML>
<script language=javascript>
            document.all("txtCustomerName").focus();
            </script>
0
 

Author Comment

by:gbmcneil
ID: 12312342
Why should you be sorry for the delay. You are the only one who has helped me thus far.  I appreciate it.

Maybe there are other geniuses out there who can help.

Adding "document.all("txtCustomerName").focus()" at the end of the /HTML tag for some reason doesn't work.

It works in other areas of my client-side code and your approach is obviously an attempt to trigger focus on the client-side after the page has been loaded.

I wonder, is there any event that VBScript might respond to once the page has been shown. In otherwords, whether there is another angle to use?






0
 
LVL 15

Expert Comment

by:praneetha
ID: 12312398
>>It works in other areas of my client-side code

when does it work and when it doesn't...

i mean if u notice i said language is javascript...

<script language=javascript>
   document.all("txtCustomerName").focus();
</script>

0
 

Author Comment

by:gbmcneil
ID: 12319418
I have done a "file save" of the code in run mode and discovered that the javascript script is showing up BEFORE the final </HTLM> tag.

So, your approach no doubts works. It is me who is at fault. I am going to have to track down why the output version is being changed. There must be a missing tag elsewhere in the page.

I will be back to you after I've figured out where my HTML has gone wrong.
0
 

Author Comment

by:gbmcneil
ID: 12320922
No matter what I did. The javascript was always moved in the final code to a place immediately before the </Body> and <\HTML> tags.  Amazing enough, however, it was called. I dropped an alert() in there and I caught it.

Nevertheless, focus never shifted in the form at startup. I could shift it in client-side code later in my application, but when the server did its thing, I couldn't get focus to direct itself to a particular control at launch. This problem was probably due to a third party control taking over and shifting focus for me after your javascript executed.

To get around it, I tried adding an "Onload" to the <Body> tag. So it became:

<Body Onload="Init()"  .......>

And, in Init() I dropped in your "document.all("txtCustomerName").focus()". Then, it worked!

Thanks again for your help. I would never have experimented if you didn't get me started.


0
 
LVL 15

Expert Comment

by:praneetha
ID: 12320992
ok cool...

i have an other idea...

you can place a literal control at the end of form in html..

and in codebehind...

literal.text="<script language=javascript> document.all('txtCustomerName').focus();</script>";

but it's ok since u allready fixed it...

good luck
have a good weekend
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question