Asp.Net :CheckBoxList用法

來源:互聯網
上載者:User
string SupporFacilities_Value = "";            //for (int i = 0; i < SupporFacilities_CheckBoxList.Items.Count; i++)            //{            //   if (SupporFacilities_CheckBoxList.Items[i].Selected)            //       SupporFacilities_Value += SupporFacilities_CheckBoxList.Items[i].Text + ",";            //}            foreach (ListItem item in SupporFacilities_CheckBoxList.Items)            {                if (item.Selected == true)                    SupporFacilities_Value += item.Text + ",";            }

                                                                                                  

for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
      if (CheckBoxList1.Items[i].Selected)
      Response.Write("你選的是" +CheckBoxList1.Items[i].Value+ CheckBoxList1.Items[i].Text + "<br>");
}
利用迴圈來順序遍曆每個選項,被選中的輸出.

                  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;

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;
      }       
    }

    資料繫結
checkedListBox1.DataSource=ds.Tables[0];
checkedListBox1.ValueMember="intSectionID";
checkedListBox1.DisplayMember="txtShortDesc".ToString();
資料顯示
int count = checkedListBox1.Items.Count;
for (int i = 0;i<count;i++)
{
if (checkedListBox1.GetItemChecked(i))
{
MessageBox.Show(checkedListBox1.Items[i].ToString());
}
}

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

反向選擇

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

checkboxlist控制項用法範例

範例一:
<script language="c#" runat="server">
public void Item_changed(Object sender,EventArgs e)
{
string str;
foreach(ListItem item in cblist.Items)
{
    if(item.Selected)
      mylabel.Text+="<hr><li>"+item.Text;
}
}
</script>
<html>
<head><title>checkbox test page</title></head>
<body>
<form runat="server">
<asp:checkboxlist id="cblist" runat="server">
<asp:listitem text="checkbox1"/>
<asp:listitem text="checkbox2"/>
<asp:listitem text="checkbox3"/>
<asp:listitem text="checkbox4"/>
</asp:checkboxlist>
<asp:button id="btn1" text="click me" OnClick="Item_changed" runat="server"/>
<hr>
<asp:label id="mylabel" runat="server"/>
</form>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.