private void PopulateIt(CheckBoxList chk,string dataTextField,
string dataValueField,sqlRawItems) //I don't really know what sqlRawItems is
{
chk.DataSource = dataTable.dbdata(sqlRawItems, 1);
chk.DataTextField = dataTextField;
chk.DataValueField = dataValueField;
chk.DataBind();
}
然後就可以調用它,無論你需要它。
PopulateIt(CheckBoxList1,"StudentName","StudentID",sqlRawItems);
PopulateIt(CheckBoxList2,"StudentName","StudentID",sqlRawItems);
PopulateIt(CheckBoxList3,"StudentName","StudentID",sqlRawItems);
PopulateIt(CheckBoxList4,"StudentName","StudentID",sqlRawItems);
PopulateIt(CheckBoxList5,"StudentName","StudentID",sqlRawItems);
我不知道怎麼ASP.Net
的作品,但在WinForm的,你可以通過你的控件
foreach (CheckBox chk in this.Controls.OfType<CheckBoxList>())
{
//Of cource assuming that all the controls will bind by same data
PopulateIt(chk ,"StudentName","StudentID",sqlRawItems);
}
他們都填充了完全相同的數據循環做財產以後也是這樣嗎? –
不,我有一個類可以傳遞sql參數來獲取他們自己的數據。 –