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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem trapping nulls in onload script.

Status
Not open for further replies.

termid0g3

MIS
Jul 14, 2003
4
US
I have the following tidbit of script that runs in the body onload of an ASP.NET page.

Document.forms errors out every time ctl is null or blank:

document.forms(...).elements(...) could not be found

Am I doing something wrong syntactically?

Thank you!!
Paul

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Status Tracker</title>
<link href="styles.css" type="text/css" rel="stylesheet" />
<script language="javascript">
function setFocus()
{
ctl = "<%= controlToFocus %>";
if (ctl != "") {
if (ctl != null) {
document.forms("TimeEntry").elements(ctl).focus();
}
}
}
</script>
</head>
<body onload="setFocus();">
 
My interpretation of the error message you're getting is that it can't find the element you're requesting to send focus to.

Add an alert to display the value of ctl after you assign it <%= controlToFocus %>. Make sure this value is actually one of the form elements (by NAME).

'hope that helps.

--Dave
 

You're using incorrect syntax to access your form elements. Replace the round brackets with square brackets... So this:

Code:
document.forms("TimeEntry").elements(ctl).focus();

becomes this:

Code:
document.forms["TimeEntry"].elements[ctl].focus();

Hope this helps,
Dan
 

You can also replace this:

Code:
if (ctl != "") {
            if (ctl != null) {

with this:

Code:
if (ctl != '' && ctl != null) {

Dan
 
Dan,

I thought it might be a square-bracket problem also, but if that were the case, I think the error message would have flagged document.forms(...) as being in error, before it even got to document.forms(...).elements(...).

So I tried this experiment:

Code:
<html>
<head>
<script>
function displayText()
{
 [b]//note the use of parentheses[/b]
 alert(document.forms('form1').elements('text1').value);
}
</script>
</head>
<body>
<form name='form1'>
<input type='text' size='30' name='text1' />
<input type='button' value='display text' onclick='displayText()' />
</form>
</body>
</html>

When I type something in the text field and press the button, the script correctly finds the value.

I tried it by using indexes as well:

document.forms(0).elements(0).value

...and it still works.

This is only IE6 that I've tried this in and perhaps the parentheses don't have cross-browser appeal, but I think termid0g3's error message would be more like 'Object expected' if it didn't recognize the parentheses.

--Dave
 
Okay, I solved the problem. CTL is assigned a value in the datagrid prerender and it's NEVER blank or null.

I added a length check to CTL (if ctl.length > 9) and tested again. It works perfectly.

Thanks to everyone for all of your help.
Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top