ASP. NET: checkboxlist usage

Source: Internet
Author: User
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













Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.