如何操作复选框中所选值的列表 - C#

问题描述 投票:0回答:1

我已经为用户进行了多种选择设置了以下方法,并将其以CSV格式存储在数据库中。

<div class="col-md-10" style="padding-top: 7px;">
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataTextField="language" DataValueField="language" AutoPostBack="True" RepeatLayout="OrderedList" Width="432px">
    <asp:ListItem Value="Saturday">Saturday</asp:ListItem>
    <asp:ListItem Value="Sunday">Sunday</asp:ListItem>
    <asp:ListItem Value="Monday">Monday</asp:ListItem>
    <asp:ListItem Value="Tuesday">Tuesday</asp:ListItem>
    <asp:ListItem Value="Wednesday">Wednesday</asp:ListItem>
    <asp:ListItem Value="Thursday">Thursday</asp:ListItem>
    </asp:CheckBoxList>
</div>

private string CheckBoxListDataToCSV() 
{            
    string selectedItems = String.Join(",",
    CheckBoxList1.Items.OfType<ListItem>().Where(r => r.Selected)
        Select(r => r.Text));
    return selectedItems;
}

string str = CheckBoxListDataToCSV(); //This method to Add in datarow

我的问题是如何在添加/编辑活动执行时填充其复选框中的值。提前感谢

c# asp.net checkbox checkboxlist
1个回答
0
投票

然后返回包含csv的数据集

DataTable dt = dsPage.Tables[0];
            string strArr = dt.Rows[0]["Training_Days"].ToString();
            string[] Arr = strArr.Split(',');

            for (int j = 0; j < Arr.Length; j++)
            {
                for (int i = 0; i < Arr.Length; i++)
                {
                    if (CheckBoxList1.Items.FindByText(Arr[i]) != null)
                    {
                        CheckBoxList1.Items.FindByText(Arr[i]).Selected = true;
                        continue;
                    }
                }
            }
© www.soinside.com 2019 - 2024. All rights reserved.