Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Grant me the serenity to accept the things I cannot change, the courage to change the things I cannot accept and the wisdom to hide the bodies of those people I had to kill today cos they pi**ed me off.
Posted
Just use the Attributes collection:
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.

Posted
Thanks! So obvious - cant believe I forgot about that one....
Grant me the serenity to accept the things I cannot change, the courage to change the things I cannot accept and the wisdom to hide the bodies of those people I had to kill today cos they pi**ed me off.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...