关于CheckBoxList点击多选的时候值可以保存到数据库中的一个字段中,同时在编辑读取的时候可以把数据库的值赋给CheckBoxList

来源:互联网 发布:linux ext3是干嘛的 编辑:程序博客网 时间:2024/06/03 15:04

 <asp:CheckBoxList ID="RblConContractor" runat="server" RepeatDirection="Horizontal">
                            <asp:ListItem Value="0">施工</asp:ListItem>
                            <asp:ListItem Value="1">维修</asp:ListItem>
                            <asp:ListItem Value="2">保养</asp:ListItem>
                            <asp:ListItem Value="3">巡检</asp:ListItem>
                            <asp:ListItem Value="4">调试值守</asp:ListItem>
                            <asp:ListItem Value="5">其他</asp:ListItem>
                        </asp:CheckBoxList>


  在后台保存方法中  保存数据库中的

   

                       string title = "";
        if (RblConContractor.SelectedIndex == -1)
        {
            model.ConServiceType = "-1";//没有选中
        }


        else for (int i = 0; i < this.RblConContractor.Items.Count; i++)
            {


                if (this.RblConContractor.Items[i].Selected == true)
                {
                    title += RblConContractor.Items[i].Value + ",";
                }


            }
        model.ConServiceType = title;





        关于CheckBoxList从数据库中获取选中的值

 


      string RdContractor = model.ConServiceType;
                if (model.ConServiceType == "-1") { }//没有选中任何
                else
                {
                    string[] arr = model.ConServiceType.Split(',');
                    foreach (string s in arr)
                    {
                        for (int j = 0; j < RblConContractor.Items.Count; j++)
                        {
                            if (s == RblConContractor.Items[j].Value)
                            {
                                RblConContractor.Items[j].Selected = true;
                            }
                        } 
                    }
                }

  

阅读全文
0 0
原创粉丝点击