Custom Validator for a checkboxlist control – Strange behavior.

I

Itai

I am attempting to create an ASP.NET Custom Validator javascript for a
checkboxlist control. My goal is to limit the total number of
selections to be 1 - 5 at most. My problem is that I get a null
reference when I attempt to retrieve an object for an individual list
item, which of course results
in an "object required" error message.

Anyone know what to do? tnx in advance

-Itai.



Here is the code:


<HEAD>

....


<script language="JavaScript">

function ChkMusicTasteSelection(source, arguments) {

var t = 0;

for(var i = 0; i < 31; i++) {

var chkbx = document.all.item("musicTaste_" + i);

// document.write(chkbx) --> null (getElementById() produces the
same effect)

if(chkbx.checked == true){
t++;
}
}

if((t <= 5) && (t >= 1)) {

arguments.IsValid = true;
}
else {
arguments.IsValid = false
}
}
}

</script>

</HEAD>


<body>
<form id="Form1" method="post" runat="server">

...


<TD style="HEIGHT: 188px"><asp:checkboxlist id="musicTaste"
runat="server"
Width="640px" RepeatColumns="5" RepeatDirection="Horizontal">

<asp:ListItem Value="1">60's</asp:ListItem>
<asp:ListItem Value="2">70's</asp:ListItem>
<asp:ListItem Value="3">80's</asp:ListItem>

...




Rendered Output: (browser ie6sp1)


<TD style="HEIGHT: 188px"><table id="musicTaste" border="0"
style="width:640px;">
<tr>
<td><input id="musicTaste_0" type="checkbox" name="musicTaste:0"
/><label for="musicTaste_0">60's</label></td>
<td><input id="musicTaste_1" type="checkbox" name="musicTaste:1"
/><label for="musicTaste_1">70's</label></td>
<td><input id="musicTaste_2" type="checkbox" name="musicTaste:2"
/><label for="musicTaste_2">80's</label></td>

...
 
L

Lee

Itai said:
I am attempting to create an ASP.NET Custom Validator javascript for a
checkboxlist control. My goal is to limit the total number of
selections to be 1 - 5 at most. My problem is that I get a null
reference when I attempt to retrieve an object for an individual list
item, which of course results
in an "object required" error message.

Anyone know what to do? tnx in advance
for(var i = 0; i < 31; i++) {

var chkbx = document.all.item("musicTaste_" + i);

// document.write(chkbx) --> null (getElementById() produces the
same effect)

It's easy to misinterpret that error as having happened on the
first time through the loop, but it may well actually have been
on the last pass.
For debugging, add the line:
alert(i + ": " + chkbx);
right after assigning the value to chkbx.

My guess is that you don't have an element named "musicTaste_30".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top