ASP.NET中如何使用CheckBoxList復(fù)選框列表控件,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
1.綁定數(shù)據(jù)
this.lngCatalogID.DataSource = dt; //這里我綁到DataTable上了. this.lngCatalogID.DataTextField = "strCatalogName"; //前臺(tái)看到的值,也就是CheckBoxList中顯示出來的值 this.lngCatalogID.DataValueField = "lngCatalogID"; //這個(gè)值直接在頁(yè)面上是看不到的,但在源代碼中可以看到 this.lngCatalogID.DataBind();
2.獲取鉤選的項(xiàng)
foreach(ListItem li in lngCatalogID.Items) { if(li.Selected) //表示某一項(xiàng)被選中了 { //li.Test表示看到的值,對(duì)應(yīng)上面的strCatalogName //li.Value表示看到的值對(duì)應(yīng)的值.對(duì)應(yīng)上面的lngCatalogID } }
3.設(shè)置某項(xiàng)為鉤選狀態(tài)
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控件用法范例
范例一、循環(huán)遍歷每個(gè)選項(xiàng),包含的對(duì)應(yīng)值的設(shè)置為選中狀態(tài)
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)來遍歷讀取每個(gè)選項(xiàng),將選中的選項(xiàng)的值拼接成字符串,以便后續(xù)插入數(shù)據(jù)庫(kù)
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)于ASP.NET中如何使用CheckBoxList復(fù)選框列表控件問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。