Solved

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

Posted on 2004-10-14
10
695 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 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