在codebehind asp.net中默认选中的checkboxlist项
在我的页面中,我有一个CheckBoxList
控件,我有7个项目。 我想在我的Page_load
codebihind中设置这7个项目。
我的页面:
Sat Sun Mon Tue Wed Thu Fri
如果你想检查一些有条件的东西,你可以使用这样的东西:
protected void Page_Load(object sender, EventArgs e) { for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if(someCondition) CheckBoxList1.Items[i].Selected = true; } }
从这里
您可以使用循环遍历 CheckBoxList
的items集合并更改Selected
属性。
foreach (ListItem item in WeeklyCondition.Items) item.Selected = true;
如何将checkboxlist项设置为默认选中
第一种方式:
Item1 Item2 Item3 Item4 Item5
第二种方式:
页面文件:
Item1 Item2 Item3 Item4 Item5
代码隐藏:
protected void Page_Load(object sender, EventArgs e) { for (int i = 0; i < CheckBoxList.Items.Count; i++) { CheckBoxList.Items[i].Selected = true; } }
Item1
如何将checkboxlist项设置为默认选中