문제

체크 박스 목록과 5 개의 레이블이 있습니다.

버튼을 클릭하면 사용자가 클릭 한 후 CheckBoxList에서 만든 5 가지 항목으로 설정할이 레이블의 텍스트 값을 원합니다.이 성취를 얻으려면 어떻게해야합니까?

미리 감사드립니다.

도움이 되었습니까?

해결책

  • bind an event to a button,
  • iterate trough the Items property of the CheckBoxList
  • set the text value according to the selected property of the listitem

like:

protected void button_Click(object sender, EventArgs e)
{
    foreach (ListItem item in theCheckBoxList.Items)
    {
        item.Text = item.Selected ? "Checked" : "UnChecked";
    }
}

to add a value you could do:

 foreach (ListItem item in theCheckBoxList.Items)
 {
        item.Text = item.Selected ? item.Value  : "";
 }

or display al values in a mini-report:

    string test = "you've selected :";
    foreach (ListItem item in theCheckBoxList.Items)
    {
        test += item.Selected ? item.Value + ", " : "";
    }
    labelResult.Text = test;

다른 팁

find selected items from CheckboxList by Lambda Linq:

var x = chkList.Items.Cast<ListItem>().Where(i => i.Selected);
    if (x!=null && x.Count()>0)
    {
         List<ListItem> lstSelectedItems = x.ToList();            
         //... Other ...
    }

Why don't you have one label and on the button click do something like:

foreach (var li in CheckList1.Items)
{
   if(li.Checked)
      Label1.Text = li.Value + "<br />";
}

That may not be the exact syntax but something along those lines.

Use this in LINQ:

foreach (var cbx3 in CheckBoxList2.Controls.OfType<CheckBox>().Where(cbx3 => cbx3.ID == s))
{
    cbx3.Checked = true;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top