This code uses Javascrip to perform operation like Select All, For eg: In screen we Have 1) Check 1 2) Check 2 3) Check 3 4) Check 4 5) Select All
These check boxes are created dynamically by ASP.NET, C# code, In client side just we need to write the javascrip code.
C#, code behind to create Dynamic check boxes
private void CreateCheckBox(string chkName,ArrayList lstCheckItems) { StringBuilder sCheckStr=new StringBuilder(); for(int i=0;i { sCheckStr.Append(""+ lstCheckItems[i] +" "); } divFurtherInfo.Text=sCheckStr.ToString(); }
Javascript to Perform 'Select All' operation, with self descriptive comments
function SelectAll(object, all) { var checkedCount=1; var selectedText=""; //get the total number of itmes in the checkboxes var count=document.getElementById("txtCountInfo").value; //if status is false set it true else set if false, but check status only onclick //of 'All the above' checkbox, and when the checked status of 'All the above' is false. if(!all) { if(state && object.id=="cblFurtherInfo"+(count-1) && document.getElementById("cblFurtherInfo"+(count-1)).checked==false) state=false; else state=true; for(i=0;i { //if 'All the above' is clicked select all the items, else even if one is unchecked //then Uncheck 'All the above' if(object.id=="cblFurtherInfo"+(count-1)) document.getElementById("cblFurtherInfo"+i).checked=state; else document.getElementById("cblFurtherInfo"+(count-1)).checked=false; //this is to get all the selected items, and store it as semicolon seperated in a variable, //to save in database, but dont capture 'All the above'. if(document.getElementById("cblFurtherInfo"+i).checked==true && document.getElementById("cblFurtherInfo"+i).value!="All the above") { selectedText=selectedText+document.getElementById("cblFurtherInfo"+i).value+";"; checkedCount=checkedCount+1; } } //check if all the checkbox is selected then select 'All the above' check box also, //if all are selected then selectedCount will be equal to Total number of items-1(Excluding 'All the above') if(checkedCount==count) document.getElementById("cblFurtherInfo"+(count-1)).checked=true; //Assigining the value of all the selected items in the hidden text box, to use in CodeBehind document.getElementById("txtSelectedFurtherInfo").value=selectedText; } //this is used for page load event, where we have to select all the items. else { for(i=0;i { //selecting all the checkboxes to true and assigning the selected values to hidden text field document.getElementById("cblFurtherInfo"+i).checked=true; if(document.getElementById("cblFurtherInfo"+i).value!="All the above") selectedText=selectedText+document.getElementById("cblFurtherInfo"+i).value+";"; } document.getElementById("txtSelectedFurtherInfo").value=selectedText; } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|