• 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>";
  • 相关阅读:
    ACM成长之路
    洛谷P1047 校门外的树
    洛谷P1046 陶陶摘苹果
    2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F题
    图论:POJ2186-Popular Cows (求强连通分量)
    DFS:POJ1562-Oil Deposits(求连通块个数)
    DFS:POJ3620-Avoid The Lakes(求最基本的联通块)
    map函数的应用:UVa156-Ananagrams
    set的应用:UVa10815-Andy's First Dictionary
    水题:UVa253-Cube painting
  • 原文地址:https://www.cnblogs.com/hz1234/p/5095077.html
Copyright © 2020-2023  润新知