Tim Maxey .NET Technology Blog & Resources

Javascript select ASP.NET Checkboxlist items

Ok I needed a function that would select certain CheckBoxList items of an ASP.NET CheckBoxList, so here it is, hope is helps someone else out!

if you want I use this function hightlight the item in yellow:

function uStyle(cb) {
   cb.parentNode.style.backgroundColor = cb.checked ? "yellow" : "";
   cb.parentNode.style.color = cb.checked ? "blue" : "";
}

function doCertainOnes(cbControl) {

    var chkBoxList = document.getElementById(cbControl);
    var chkBoxCount = chkBoxList.getElementsByTagName("input");

    chkBoxCount[5].checked = true;
    chkBoxCount[5].focus();    //GOING TO JUMP DOWN IN THE LIST TO THE FIRST SELECTED       
    uStyle(chkBoxCount[5]);
    chkBoxCount[6].checked = true;
    uStyle(chkBoxCount[6]);
    chkBoxCount[7].checked = true;
    uStyle(chkBoxCount[7]);

  
  for (var i = 10; i < 50; i++) {
       chkBoxCount[i].checked = true;
       uStyle(chkBoxCount[i]);
    }
}

I am selecting items (array) 5, 6, 7 and 10 through 49 in the list, now you can repeat the for loop and select different ones to. I happen to know that there will always be 100 items in the checkbox list and which ones they are... I use the "focus()" function to jump to the first item selected in the list...

Use it like this:

<a href="JavaScript:doCertainOnes('<% =CheckBoxList1.ClientID %>');">Select My Favs</a>


Feedback

# re: Javascript select ASP.NET Checkboxlist items

Gravatar I've got a problem when using listview's insertitem template wrappered in updatepanel, why I can't check the source code part of "insert,cacel" segment in browser ? I mean if using updatepanel to enable Ajax, hit btnAdditem button, for example, then, a insert item template displays at the bottom of item template, then, maybe I want to add some calendarextender or jquery's datepicker to some textbox (i.e, bind to the birthday field back in the database), because I can not find the source code of insertitem in browser, I even don't know how to do next.
3/15/2010 8:01 AM | Texas holdem spielen

Post a comment





 

Please add 1 and 8 and type the answer here: