Checkboxes with javascript functionality

bizzydint

Regular
Joined
Apr 8, 2003
Messages
75
I'm attempting to have a bit of javascript that counts how many checkboxes are ticked and displays a simple alert when the user goes over the specified maximum. That part is simple.

It gets slightly more complicated in that there are several lists of items with different maximums on each list (eg select 2 items from List A and select 5 items from List B etc). So the javascript simply calls the functions with "CountChecked(this, maxCountAllowed, GroupID)" and it works out what it's doing. Still simple.

The problem is that all the data comes from a database and is completely variable.

The asp.net checkbox control is letting me have a "onClick" command (although it's grumbling that it doesnt support it but it lets it through anyway) - but I cant seem to put variables in there...

This works fine:
<asp:CheckBox ID="cbx"
onClick='CountChecks(this, 3, 1)'
Runat=server>
</asp:CheckBox>

But not this:
<asp:CheckBox ID="cbx"
onClick='CountChecked(this, 3, <%#groupID %>)'
Runat=server ></asp:CheckBox>

It doesnt error - it simply writes "<% # groupID %>" onto the HTML instead of working out what the value of groupID is and putting that in.

It's all databound etc.

Am I missing something obvious?
 
Just use the Attributes collection:
Code:
CheckBox1.Attributes.Add("onClick","CountChecked(this, 3, " + groupID + ")");
If you check out the generated HTML on the client side you will see that this accomplishes what you need.
 
Back
Top