网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 ASP.NET中CheckBoxList复选框列表控件详细使用方法
内容
    本文主要介绍CheckBoxList几种常见的用法,并做出范例演示供大家参考,希望对学习asp.net的朋友有所帮助。
    可以使用两种类型的 ASP.NET 控件将复选框添加到 Web 窗体页上:单独的 CheckBox 控件或 CheckBoxList 控件。两种控件都为用户提供了一种输入布尔型数据(真或假、是或否)的方法。
    本文主要介绍CheckBoxList,不言而喻,看到List就知道是一个列表(集合),一个控件可以包含多个CheckBox,下面让我们来看看具体的用法。
    1.绑定数据
    代码如下:
    this.lngCatalogID.DataSource = dt; //这里我绑到DataTable上了.
    this.lngCatalogID.DataTextField = "strCatalogName"; //前台看到的值,也就是CheckBoxList中显示出来的值
    this.lngCatalogID.DataValueField = "lngCatalogID"; //这个值直接在页面上是看不到的,但在源代码中可以看到
    this.lngCatalogID.DataBind();
    2.获取钩选的项
    代码如下:
    foreach(ListItem li in lngCatalogID.Items)
    {
        if(li.Selected)    //表示某一项被选中了
        {   
            //li.Test表示看到的值,对应上面的strCatalogName
            //li.Value表示看到的值对应的值.对应上面的lngCatalogID
        }
    }
    3.设置某项为钩选状态
    代码如下:
    foreach(ListItem li in lngCatalogID.Items)
    {
        if(li.Value.Equals("钩选条件"))    //如果li.Value值等于某值,就钩选
        {
            li.Selected = true;                    //等于true就表示钩选啦.
            break;
        }
    }
    4.DataGrid中全选
    代码如下:
    foreach(DataGridItem thisItem in DataGridLogininfo.Items)
    {
        ((CheckBox)thisItem.Cells[0].Controls[1]).Checked = CheckBox2.Checked;
    }
    5.反向选择
    代码如下:
    for (int i = 0; i < checkedListBox1.Items.Count; i++)
    {
        if (checkedListBox1.GetItemChecked(i))
        {
            checkedListBox1.SetItemChecked(i, false);
        }
        else
        {
            checkedListBox1.SetItemChecked(i, true);
        }
    }
    CheckBoxList控件用法范例
    范例一、循环遍历每个选项,包含的对应值的设置为选中状态
    代码如下:
    for (int i = 0; i < hfAnswers.Value.Split(',').Length; i++)//给CheckBoxList选中的复选框 赋值
    {
        for (int j = 0; j < CBoxListAnswer.Items.Count; j++)
        {
            if (hfAnswers.Value.Split(',')[i] == CBoxListAnswer.Items[j].Value)
            {
              CBoxListAnswer.Items[j].Selected = true;
            }
        }
    }
    范例二、循环来遍历读取每个选项,将选中的选项的值拼接成字符串,以便后续插入数据库
    代码如下:
    string m_strTemp = string.Empty;
    for (int i = 0; i < CBoxListAnswer.Items.Count; i++)//读取CheckBoxList 选中的值,保存起来
    {
        if (CBoxListAnswer.Items[i].Selected)
        {
            m_strTemp += CBoxListAnswer.Items[i].Value + ",";
        }
    }
    if (!string.IsNullOrEmpty(m_strTemp))
        Label1.Text = m_strTemp.Substring(0, m_strTemp.Length - 1);
    else
        Label1.Text = m_strTemp;
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/22 5:15:44