asp.net implementation of the group selection check box
Source: Internet
Author: User
1. Declare the control, at the top of the page a checkbox called CB5, a checkboxlist called Shi2:
protected System.Web.UI.WebControls.CheckBox cb5;
protected System.Web.UI.WebControls.CheckBoxList Shi2;
2. Type the following function in the head of the ASPX HTML code
<script>
function Chkchk (Chkid,spanid) {
var O=document.getelementbyid (Chkid);
var obj = document.getElementById (Spanid);
Obj=obj.children;
for (i=0;i<obj.length;i++)
{
E=obj[i];
if (e.type== "checkbox") {
obj[i].checked=o.checked;
}
}
}
</script>
3. Add custom attributes to the background code
private void Page_Load (object sender, System.EventArgs e)
{
Cb5. Attributes.Add ("onclick", "Chkchk" ("+CB5"). Clientid+ "', '" +shi2. Clientid+ "");
}
4. We'll look at some auto-generated HTML code, and we'll see
<input id= "to1_cb5" type= "checkbox" Name= "TO1:CB5" onclick= "Chkchk" (' to1_cb5 ', ' to1_shi2 '); "/>
<label for= "TO1_CB5" > City Bar Association </label>
<span id= "To1_shi2" >
<input id= "to1_shi2_0" type= "checkbox" Name= "to1:shi2:0"/>
<label for= "To1_shi2_0" >admin</label>
<br>
<input id= "to1_shi2_1" type= "checkbox" Name= "To1:shi2:1"/>
<label for= "To1_shi2_1" >5</label>
</span>
5. Note I put all the controls in a to1 UserControl inside, so the generated control ID is preceded by a "to1_", but also an afternoon time, the whole out of a this, the total sense of asp.net to some client-side programming is not very good, if not write script, If you make a linkage selection, you can only use server events, this will be every click check box to return to the form, which is very not worth considering the performance, can only do this. The main knowledge points of this article are JavaScript document objects and the nested model of the controls, plus some client behavior properties for server controls .
Finally, thanks to the technical support of Henman sister of Donet Technology Group
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.