From Article http://www.experts-exchang
============
Well, here's another try. This might work, but only IE supports this syntax. If it works, a similar solution can be made to support other browsers.
Two possible ways of going at it:
1. Give your single unique checkbox an id.
<asp:ListItem id="SpecialCheckbox"...>
Then in the <head> section of your aspx page, add the following client-side script block:
<script for="SpecialCheckbox" event="onclick">
window.cancelEvent = true; // avoid the postback
var chk = event.srcElement;
alert( chk.checked ); // just to see that it works
</script>
2. This is a better solution for generated list items (i.e., made at run-time in your code-behind or other scriptblock):
Give all your listitems the same id, and also give them an attribute that identifies them in addition.
So the result html could look like:
<input type="checkbox" name="MyCheckboxList" specialId="1">
<input type="checkbox" name="MyCheckboxList" specialId="2">
<input type="checkbox" name="MyCheckboxList" specialId="3">
And then in the same client-side script block:
<script for="SpecialCheckbox" event="onclick">
var chk = event.srcElement;
alert( chk.checked ); // just to see that it works
if( chk.specialId == '3' ) {
event.cancelEvent = true; // avoid the postback
// do stuff
}
</script>
If these don't work, you can try some variation. Maybe this will give you a better direction.
Btw, have you considered that a better solution may be to avoid using a CheckBoxList? You can use a DataRepeater to render all your checkboxes in a similar way, and this gives you more flexibility for particular treatment of special checkboxes.
Main Topics
Browse All Topics





by: quanmacPosted on 2009-09-01 at 10:10:47ID: 25233658
I've also tried:
unCheck.checked = false;
in the javascript and it still didn't work.