.net 後臺獲取CheckBoxList的值
1.前臺的多選控制元件 <asp:CheckBoxList runat="server" ID="cblStr" RepeatDirection="Horizontal" > </asp:CheckBoxList>
2.後臺獲取多選的值
string str= string.Empty;
foreach (ListItem li in cblStr.Items)
{
if (li.Selected == true)
{
str+= li.Value + ",";
}
}
str.TrimEnd(','); 就是你需要的多選值
第三種:給CheckBoxList賦值
foreach (string str in split)
{
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Text == str)
{
CheckBoxList1.Items[i].Selected = true;
}
}
}
第四種:判斷是否選中
var flag1=0;
var checkobj = document.getElementById("CheckBoxList1");
var checks = checkobj.getElementsByTagName("input");
for(var n=0;n<checks.length;n++)
{
if(checks[n].type=="checkbox" && checks[n].checked==true)
{
flag1=1;
}
}
if(flag1==0)
{
alert("請選擇欄目設定!");
return false;
}