20151215单选按钮列表,复选框列表:CheckBoxList

单选框:RadioButton
    GroupName:组名,如果要实现单选效果每个单选按钮的组名必须一样
    是否被选中 RadioButton.checked

单选按钮列表:RadioButtonList
    属性:RepeatDirection:Vertical上下 Honizontal左右
    绑定数据源:
                DataClassesDataContext context = new DataClassesDataContext();
                RadioButtonList1.DataSource = context.Nation;
                RadioButtonList1.DataValueField = "Code";
                RadioButtonList1.DataTextField = "Name";
                RadioButtonList1.DataBind();
    取选中项的值:
        RadioButtonList1.SelectedValue.ToString();
    设置哪一项被选中:
        RadioButtonList1.SelectedIndex=2;

复选框:CheckBox
    是否被选中 CheckBox1.checked

复选框列表:CheckBoxList
    属性:RepeatDirection:Vertical上下 Honizontal左右
    绑定数据:
        CheckBoxList1.DataSource = context.Nation;
        CheckBoxList1.DataValueField = "Code";
        CheckBoxList1.DataTextField = "Name";
        CheckBoxList1.DataBind();
    取选中项的值:
                foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected)
            {
                Label1.Text += item.Text;
            }
        }

    设置哪一项被选中:
        如果是一项选中:SelectedIndex=2;
        如果是多想选中:
                  foreach (ListItem item in CheckBoxList1.Items)
                        {
                            if (item.Text == "汉族" || item.Text == "满族")
                            {
                                item.Selected = true;
                            }
                         }



练习:
    全选:  foreach (ListItem item in CheckBoxList1.Items)
                {
                    item.Selected = CheckBox1.Checked;
                }

    弹窗:
         string zhi="";
        foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected)
            {
                zhi += item.Value.ToString()+",";
            }
        }
        //去掉最后多余的逗号
        zhi = zhi.Substring(0, zhi.Length - 1);
        //拆分字符串
        string[] codes;
        codes = zhi.Split(',');
        //造JS代码
        string js = "<script type='text/javascript'>";

        for (int i = 0; i < codes.Length; i++)
        {
            js += "alert('" + codes[i] + "');";
        }
        js += "</script>";

        Literal1.Text = js;
        //Literal1.Text = "<script type='text/javascript'>alert('" + zhi + "')</script>";
原文地址:https://www.cnblogs.com/hz1234/p/5095077.html