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

后端 未结 6 2094
盖世英雄少女心
盖世英雄少女心 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:34

    An elegant way to implement this would be to make an extension method, like this:

    public static class Extensions
    {
        public static List GetSelectedItems(this CheckBoxList cbl)
        {
            var result = new List();
    
            foreach (ListItem item in cbl.Items)
                if (item.Selected)
                    result.Add(item.Value);
    
            return result;
        }
    }
    

    I can then use something like this to compose a string will all values separated by ';':

    string.Join(";", cbl.GetSelectedItems());
    

提交回复
热议问题