Solved

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

Posted on 2004-10-14
10
726 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

626 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