Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie question on setting focus

Status
Not open for further replies.

newora

Programmer
Aug 19, 2003
133
GB
Hi There,

Apologies for this simple and stupid question but I have only just started looking into ASP .NET (been meaning to do it for 3 years and only just found some spare time in between projects !!)

I am trying to develop a small web interface that will allow me to setup / change registry settings for an existing application.

I have one web page with just a textbox and a button on it at the moment. When I click the button I want to place the focus to the textbox, so that I can enter the required configuration data. I am so dumb that I can not even get this to work !!

I am using ASP .NET 1.1 under VS 2003.

In the code behind module for the web page I have the following function defined :-

Private Sub SetFocus(ByVal ctrl As System.Web.UI.Control)
Dim s As String
s = "<SCRIPT language = 'javascript'>document.getElementById('" & ctrl.ID & _
"').focus()</SCRIPT>"
RegisterStartupScript("focus", s)
End Sub

Then in the button click event I call the function setfocus, passing it the name of the ASP textbox control that is one the web form.

However when I run the web site, I get an error saying that the object doesn't support this property or method.

If anyone could please give me any pointers, it would be much appreciated, as I seem to be falling down at the first hurdle on trying to do something so simple!!

Thanks for your help
 
You'll need to have a look at the markup that is sent to the client. My guess is that the ID that you are sending to the client isn't correct. If this is true, try using the ClientID property of the control rather than the ID.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi, Thanks for that - I have actually tried using clientid before and got the same results.

Looking at the javascript side, it seems OK. I have a breakpoint in the setfocus procedure and it shows :-

<SCRIPT language = 'javascript'>
document.getElementById('txtmetric').focus()
</SCRIPT>

The control that I want to set focus to seems OK, in that it is called txtmetric.

I must be missing something somewhere as it seems that the command is being parsed by the VB .NET side of things and
not the Javascript side - id that makes sense!!
 
If it isn't working (i.e. you are still getting an "object doesn't support this property or method" javascript error) then the javascript method probably isn't finding the control. I'd ask in the javascript forum as this is now not an ASP.NET issue as you've said the correct ID and javascript is being written out to the page so ASP.NET has done it's job and the error is elsewhere.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top