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

'Undefined' error 1

Status
Not open for further replies.

Peppi

Programmer
Joined
Apr 9, 2001
Messages
205
Location
CA
Hi,

I have an ASP.NET page into which I programmatically insert some JavaScript code to some textboxes for each row in a GridView:

Code:
chkChanged = CType(e.Row.FindControl("chkChanged"), CheckBox)
leaveDateTextBox = CType(e.Row.FindControl("txtLeaveDate"), TextBox)
returnDateTextBox = CType(e.Row.FindControl("txtReturnDate"), TextBox)
leaveDateTextBox.Attributes.Add("onChange", "return markAsChanged(" + chkChanged.ClientID + ")")
returnDateTextBox.Attributes.Add("onChange", "return markAsChanged(" + chkChanged.ClientID + ")")

However, I am getting the following error when the JavaScript runs:
'ctl00_Main_grdVacation_ctl02_chkChanged' is undefined.

When I view the source, I do indeed have a checkbox with that id:

Code:
<span style="font-size:10px;">
<input id="ctl00_Main_grdVacation_ctl02_chkChanged" type="checkbox" name="ctl00$Main$grdVacation$ctl02$chkChanged" />
</span>

And where it is being passed as a parameter:

Code:
<input name="ctl00$Main$grdVacation$ctl02$txtReturnDate" type="text" value="2007/01/02 12:00:00 AM" id="ctl00_Main_grdVacation_ctl02_txtReturnDate" onChange="return markAsChanged(ctl00_Main_grdVacation_ctl02_chkChanged)" style="font-size:10px;width:64px;" />

Would anyone happen to know why it is giving me this undefined error when I have an element with that ID? The only thing I can think of that might possibly be causing it is that I have the grid inside of a panel, which is nested inside an AJAX UpdatePanel. These panels then translate into <div> tags. But I still don't know what the solution could be.

Thx.
 
Right here:

Code:
<input name="ctl00$Main$grdVacation$ctl02$txtReturnDate" type="text" value="2007/01/02 12:00:00 AM" id="ctl00_Main_grdVacation_ctl02_txtReturnDate" onChange="return markAsChanged([!]'[/!]ctl00_Main_grdVacation_ctl02_chkChanged[!]'[/!])" style="font-size:10px;width:64px;" />
Assuming you are passing the span ID as a parameter, it is passed as a string, not a variable.

However, if you are wanting to pass the object you need to do it like this:

Code:
<input name="ctl00$Main$grdVacation$ctl02$txtReturnDate" type="text" value="2007/01/02 12:00:00 AM" id="ctl00_Main_grdVacation_ctl02_txtReturnDate" onChange="return markAsChanged([!]document.getElementById('[/!]ctl00_Main_grdVacation_ctl02_chkChanged[!]')[/!])" style="font-size:10px;width:64px;" />

By the way, those ID's and Names are HUUUUUGEE.

[small]"There's an old saying in Tennessee — I know it's in Texas, probably in Tennessee — that says, fool me once, shame on — shame on you. Fool me — you can't get fooled again." - George W. Bush[/small]
<.
 
Thanks! That worked.
Those names and IDs are auto-generated to make them unique, so not much I can do about that.
 
Ahh I see, my knowledge on ASP.NET is nonexistant.

[small]"There's an old saying in Tennessee — I know it's in Texas, probably in Tennessee — that says, fool me once, shame on — shame on you. Fool me — you can't get fooled again." - George W. Bush[/small]
<.
 
That's OK. Your knowledge of JavaScript makes up for it. [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top