RadioButtonList 和CheckBoxList根据后天数据库的值加载时选中其选项

前台:

<x:RadioButtonList ID="rbSex" Label="性别" runat="server"  >
                                       <x:RadioItem Text="男" Value="男" Selected="true"  />
                                        <x:RadioItem Text="女" Value="女" />
                                    </x:RadioButtonList>

前台:  <x:CheckBoxList ID="cbl" runat="server" ColumnNumber="2" Label="角色" ></x:CheckBoxList>

绑定数据:  protected void inti()
        {
            List<RoleInfo> roleList = roleDAL.List();
            if (roleList == null)
            {
                Alert.Show("加载角色信息失败!", MessageBoxIcon.Warning);
            }
           
            cbl.DataTextField = "Name";
            cbl.DataValueField = "ID";
         
            cbl.DataSource = roleList;
            cbl.DataBind();
        }

RadioButtonList根据数据库的值加载显示选中:

   if (userInfo.Sex.Equals("男"))
                    {
                        rbSex.SelectedIndex=0;
                    }
                    else
                    {
                        rbSex.SelectedIndex = 1;
                       
                    }

CheckBoxList根据数据库的值加载显示选中:

 List<RoleInfo> list = roleDAL.ListRoleByUserID(new Guid(Request.QueryString["id"]));
                        if (list != null)
                        {
                            
                            string[] arry = cbl.SelectedValueArray;//cbl的值选项
                            for (int i = 0; i<roleDAL.List().Count; i++)//i<角色信息表的总数量条数。
                            {
                                if (cbl.Items[i].Value == list.FirstOrDefault().ID.ToString())
                                {
                                    cbl.Items[i].Selected = true;
                                }
                            }
                        }

原文地址:https://www.cnblogs.com/fang645421992/p/3818887.html