Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Custom Validator for a checkboxlist control – Strange behavior.

Reply
Thread Tools

Custom Validator for a checkboxlist control – Strange behavior.

 
 
Itai
Guest
Posts: n/a
 
      07-18-2004
I am attempting to create a Custom Validator client script 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>

...
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      07-19-2004
Hi,

first of all, the code for client-side validation function had one closing
curly brace too much on the end of the function. Second that do you have 30
or 31 ListItems on CheckBoxList? Note that indexing starts for ids at zero,
so could it be that the loop should be:

///
for(var i = 0; i < 30; i++) {
....
}
///

And third, I used just this for referencing each individual checkbox:

///
var chkbx = document.all["musicTaste_" + i];
///

but this worked equally:

///
var chkbx = document.getElementById("musicTaste_" + i);
///

So therefore, do check your loop with extra care (and remove the extra
closing curly brace at the end of the function). If you can't otherwise get
what it tries to look for, put

///
alert(chkbx.id);
///

right after the reference for the CheckBox is acquired so that you'löl see
what it tries to look for.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke




"Itai" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I am attempting to create a Custom Validator client script 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>
>
> ...



 
Reply With Quote
 
 
 
 
Itai Itai
Guest
Posts: n/a
 
      07-22-2004
But of (by one) course…

tnx very much...

-Itai



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Itai Itai
Guest
Posts: n/a
 
      07-22-2004
But of (by one) course…

tnx very much...

-Itai



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checkboxlist inside Repeater - doesn't fire click event for Checkboxlist JD ASP .Net 5 08-08-2007 11:28 AM
How to get Selected item in a Databinded CheckBoxList when CheckBoxlist is in a DataList? Patrick.O.Ige ASP .Net 5 06-19-2005 06:11 AM
Custom Validator for a checkboxlist control – Strange behavior. Itai Javascript 2 07-22-2004 11:25 AM
Custom Validator for a checkboxlist control – Strange behavior. Itai Javascript 0 07-18-2004 02:32 PM
How to get custom control to work with custom validator Scott Yenor ASP .Net Web Controls 0 01-27-2004 12:37 AM



Advertisments