首页 > 其他 > 详细

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

时间:2016-01-02 20:29:18      阅读:658      评论:0      收藏:0      [点我收藏+]
单选框: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>";

 

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

原文:http://www.cnblogs.com/hz1234/p/5095077.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!