zoukankan      html  css  js  c++  java
  • 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>";
  • 相关阅读:
    Spring框架中的单例bean是线程安全的吗?
    Spring Cloud 解决了哪些问题?
    服务端处理 Watcher 实现 ?
    内部类可以引用它的包含类(外部类)的成员吗?有没有什么限制?
    vue-router点击同一个页面异常处理
    mybatis代码生成+自定义注解+自定义注释
    lombok使用
    idea格式化设置
    寒假每周总结4
    寒假每日日报27
  • 原文地址:https://www.cnblogs.com/hz1234/p/5095077.html
Copyright © 2011-2022 走看看