For the CheckBoxList control, on the one hand to achieve a large number of data on the server side of the binding work, on the other hand, often require the implementation of the full selection, reverse-election functions. Although this work can be done on the server side, such a simple work seems more likely to be done on the client.
Specific Methods:
Put a CheckBoxList control in the page and add a few items to analyze its resulting HTML code, so you can use JS to
When dynamic control is made, it will be very clear that the test code looks like this:
Copy Code code as follows:
<asp:checkboxlistid= "CheckBoxList1" runat= "Server" cellpadding= "3" cellspacing= "3"
repeatcolumns= "3" >
<asp:ListItem>1232</asp:ListItem>
<asp:ListItem>254</asp:ListItem>
<asp:listitemvalue= "5643" >5643</asp:ListItem>
<asp:ListItem>789</asp:ListItem>
<asp:ListItem>654</asp:ListItem>
<asp:ListItem>564</asp:ListItem>
<asp:ListItem>8564</asp:ListItem>
<asp:ListItem>8564</asp:ListItem>
<asp:ListItem>5452</asp:ListItem>
<asp:ListItem>5641</asp:ListItem>
</asp:CheckBoxList>
View in a browser and parse HTML: The following is the HTML code generated by the DropDownList control.
Copy Code code as follows:
<tableid= "CheckBoxList1" cellspacing= "3" cellpadding= "3" border= "0" >
<tr>
<td><inputid= "Checkboxlist1_0" type= "checkbox" Name= "checkboxlist1$0"/><labelfor= "CheckBoxList1_0" ">1232</label>
</td>
<td><inputid= "checkboxlist1_4" type= "checkbox" Name= "checkboxlist1$4"/><labelfor= "CheckBoxList1_4" ">654</label>
</td>
.......
</table>
Here, excerpt some of the code, where the blue part is our concern. In HTML, CheckBoxList generates a
Many input (type is a checkbox) and its ID is "checkboxlist1_i" (I is a number). So that we only
Need to know a few items can easily realize JS control of it.
These input are included in a table with an ID of CheckBoxList1, so you can pass:
Copy Code code as follows:
document.getElementById ("CheckBoxList1"). getElementsByTagName ("input"). length
This method gets the total number of CheckBoxList, the rest of the work is actually very simple, through JS change every
The state of the checkbox can be. Add three button first to implement Select, reverse, and purge control, as follows:
Copy Code code as follows:
<inputtype= "button" onclick= "Checkall ()" value= "Checkall"/>
<inputtype= "button" onclick= "Reverseall ()" value= "Reverseall" id= "Button1"/>
<inputtype= "button" onclick= "DeleteAll ()" value= "DeleteAll"/>
Add the full selection, inverse, and purge functions as follows:
Copy Code code as follows:
Functioncheckall () {
Alert (document.getElementById ("CheckBoxList1"). getElementsByTagName ("input"). length);
For (Vari=0;i<document.getelementbyid ("CheckBoxList1"). getElementsByTagName ("Input"). length;i++)
{
document.getElementById ("checkboxlist1_" +i). Checked=true;
}
}
Functiondeleteall () {
For (Vari=0;i<document.getelementbyid ("CheckBoxList1"). getElementsByTagName ("Input"). length;i++)
{
document.getElementById ("checkboxlist1_" +i). Checked=false;
}
}
Functionreverseall () {
For (Vari=0;i<document.getelementbyid ("CheckBoxList1"). getElementsByTagName ("Input"). length;i++)
{
Varobjcheck=document.getelementbyid ("checkboxlist1_" +i);
if (objcheck.checked)
Objcheck.checked=false;
Else
Objcheck.checked=true;
}
}
OK, now through IE testing, binding work can be in the background, the full selection of auxiliary functions can be free to play!