Solved

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

Posted on 2004-10-14
10
678 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
Comment Utility
document.all("txtCustomerName").focus();
0
 

Author Comment

by:gbmcneil
Comment Utility
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
Comment Utility
you would see accept answer next to my answer so u just click it and select the grade...

thank you.

good luck
0
 

Author Comment

by:gbmcneil
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:gbmcneil
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now