How can I get the CheckBoxList selected values, what I have doesn't seem to work C#.NET/VisualWebPart

后端 未结 6 2098
盖世英雄少女心
盖世英雄少女心 2020-11-30 07:53

I am creating a CheckBoxList in a class file and am using an HTMLTextWriter to render the control.

I\'m using the following code to store the selected values in a s

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 08:39

    // Page.aspx //

    // To count checklist item
    
      int a = ChkMonth.Items.Count;
            int count = 0;
    
            for (var i = 0; i < a; i++)
            {
                if (ChkMonth.Items[i].Selected == true)
                {
                    count++;
                }
            }
    

    // Page.aspx.cs //

      // To access checkbox list item's value //
       string YrStrList = "";
            foreach (ListItem listItem in ChkMonth.Items)
            {
                if (listItem.Selected)
                {
                    YrStrList = YrStrList + "'" + listItem.Value + "'" + ",";
                }
    
            }
    
            sMonthStr = YrStrList.ToString();
    

提交回复
热议问题