Solved

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

Posted on 2004-10-14
10
719 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

752 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