String supporfacilities_value = ""; // For (INT I = 0; I <supporfacilities_checkboxlist.items.count; I ++) // {// If (supporfacilities_checkboxlist.items [I]. selected) // supporfacilities_value + = supporfacilities_checkboxlist.items [I]. text + ","; //} foreach (listitem item in supporfacilities_checkboxlist.items) {If (item. selected = true) supporfacilities_value + = item. text + ",";}
For (INT I = 0; I <checkboxlist1.items. Count; I ++)
{
If (checkboxlist1.items [I]. Selected)
Response. Write ("You selected" + checkboxlist1.items [I]. Value + checkboxlist1.items [I]. Text + "<br> ");
}
Use loops to traverse each option sequentially and select the output.
For (INT I = 0; I Assign a value to the check box selected in checkboxlist
{
For (Int J = 0; j <cboxlistanswer. Items. Count; j ++)
{
If (hfanswers. value. Split (',') [I] = cboxlistanswer. items [J]. value)
{
Cboxlistanswer. items [J]. Selected = true;
}
}
}
String m_strtemp = string. empty;
For (INT I = 0; I <cboxlistanswer. Items. Count; I ++ )// Read the selected value of the checkboxlist and save it.
{
If (cboxlistanswer. items [I]. Selected)
{
M_strtemp + = cboxlistanswer. items [I]. Value + ",";
}
}
If (! String. isnullorempty (m_strtemp ))
Label1.text = m_strtemp.substring (0, m_strtemp.length-1 );
Else
Label1.text = m_strtemp;
1. Bind data
This. lngcatalogid. datasource = DT; // I bound it to the able.
This. lngcatalogid. datatextfield = "strcatalogname"; // The value displayed on the foreground, that is, the value displayed in the checkboxlist.
This. lngcatalogid. datavaluefield = "lngcatalogid"; // This value is invisible directly on the page,Source codeYou can see
This. lngcatalogid. databind ();
2. Get the checked items
Foreach (listitem Li in lngcatalogid. Items)
{
If (Li. Selected) // indicates that an item is selected
{
// Li. test indicates the value displayed, which corresponds to the strcatalogname
// Li. value indicates the value corresponding to the value you see. It corresponds to the above lngcatalogid
}
}
3. Set an item to the check status.
Foreach (listitem Li in lngcatalogid. Items)
{
If (Li. value. Equals ("hook selection condition") // If the Li. value is equal to a value, select
{
Li. Selected = true; // if it is set to true, the check box is selected.
Break;
}
}
Data Binding
Checkedlistbox1.datasource = Ds. Tables [0];
Checkedlistbox1.valuemember = "intsectionid ";
Checkedlistbox1.displaymember = "txtshortdesc". tostring ();
Data Display
Int COUNT = checkedlistbox1.items. count;
For (INT I = 0; I <count; I ++)
{
If (checkedlistbox1.getitemchecked (I ))
{
MessageBox. Show (checkedlistbox1.items [I]. tostring ());
}
}
Select All in the DataGrid
Foreach (maid)
{
(Checkbox) thisitem. cells [0]. controls [1]). Checked = checkbox2.checked;
}
Reverse Selection
For (INT I = 0; I <checkedlistbox1.items. Count; I ++)
{
If (checkedlistbox1.getitemchecked (I ))
{
Checkedlistbox1.setitemchecked (I, false );
}
Else
{
Checkedlistbox1.setitemchecked (I, true );
}
}
Checkboxlist control usage example
Example 1:
checkbox test page