欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET中CheckBoxList復(fù)選框列表控件詳細(xì)使用方法

 更新時間:2016年04月19日 09:35:24   作者:清風(fēng)夜影寒  
本文主要介紹CheckBoxList幾種常見的用法,并做出范例演示供大家參考,希望對學(xué)習(xí)asp.net的朋友有所幫助。

可以使用兩種類型的 ASP.NET 控件將復(fù)選框添加到 Web 窗體頁上:單獨(dú)的 CheckBox 控件或 CheckBoxList 控件。兩種控件都為用戶提供了一種輸入布爾型數(shù)據(jù)(真或假、是或否)的方法。

本文主要介紹CheckBoxList,不言而喻,看到List就知道是一個列表(集合),一個控件可以包含多個CheckBox,下面讓我們來看看具體的用法。

1.綁定數(shù)據(jù)

復(fù)制代碼 代碼如下:

this.lngCatalogID.DataSource = dt; //這里我綁到DataTable上了.
this.lngCatalogID.DataTextField = "strCatalogName"; //前臺看到的值,也就是CheckBoxList中顯示出來的值
this.lngCatalogID.DataValueField = "lngCatalogID"; //這個值直接在頁面上是看不到的,但在源代碼中可以看到
this.lngCatalogID.DataBind();

2.獲取鉤選的項

復(fù)制代碼 代碼如下:

foreach(ListItem li in lngCatalogID.Items)
{
    if(li.Selected)    //表示某一項被選中了
    {  
        //li.Test表示看到的值,對應(yīng)上面的strCatalogName
        //li.Value表示看到的值對應(yīng)的值.對應(yīng)上面的lngCatalogID
    }
}

3.設(shè)置某項為鉤選狀態(tài)

復(fù)制代碼 代碼如下:

foreach(ListItem li in lngCatalogID.Items)
{
    if(li.Value.Equals("鉤選條件"))    //如果li.Value值等于某值,就鉤選
    {
        li.Selected = true;                    //等于true就表示鉤選啦.
        break;
    }
}

4.DataGrid中全選

復(fù)制代碼 代碼如下:

foreach(DataGridItem thisItem in DataGridLogininfo.Items)
{
    ((CheckBox)thisItem.Cells[0].Controls[1]).Checked = CheckBox2.Checked;
}

5.反向選擇

復(fù)制代碼 代碼如下:

for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
    if (checkedListBox1.GetItemChecked(i))
    {
        checkedListBox1.SetItemChecked(i, false);
    }
    else
    {
        checkedListBox1.SetItemChecked(i, true);
    }
}

CheckBoxList控件用法范例

范例一、循環(huán)遍歷每個選項,包含的對應(yīng)值的設(shè)置為選中狀態(tài)

復(fù)制代碼 代碼如下:

for (int i = 0; i < hfAnswers.Value.Split(',').Length; i++)//給CheckBoxList選中的復(fù)選框 賦值
{
    for (int j = 0; j < CBoxListAnswer.Items.Count; j++)
    {
        if (hfAnswers.Value.Split(',')[i] == CBoxListAnswer.Items[j].Value)
        {
          CBoxListAnswer.Items[j].Selected = true;
        }
    }
}

范例二、循環(huán)來遍歷讀取每個選項,將選中的選項的值拼接成字符串,以便后續(xù)插入數(shù)據(jù)庫

復(fù)制代碼 代碼如下:

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;

相關(guān)文章

最新評論