綁定數據:
... CheckBoxList.DataSource = dt; CheckBoxList.DataTextField = "text"; CheckBoxList.DataValueField = "id"; CheckBoxList.DataBind();
讀取Value:
for (int i = 0; i < ChenkBoxList.Items.Count; i++) { if (ChenkBoxList.Items[i].Selected == true) { Label1.Text += ChenkBoxList.Items[i].Value + "|"; } }
注意:在綁定頁面的protected void Page_Load(object sender, EventArgs e)要加上海if (!IsPostBack) 如:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ... CheckBoxlist.DataSource = dt; CheckBoxlist.DataTextField = "Text"; CheckBoxlist.DataValueField = "id"; CheckBoxlist.DataBind(); ... } }