CheckBoxList控件的定义
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"
Width="180px">
</asp:CheckBoxList>
向CheckBoxList控件绑定数据
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Sample5_3
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//数据生成
DataSet ds = new DataSet();
ds.Tables.Add("stu");
ds.Tables["stu"].Columns.Add("stuNo", typeof(int));
ds.Tables["stu"].Columns.Add("stuName", typeof(string));
ds.Tables["stu"].Columns.Add("stuScore", typeof(int));
ds.Tables["stu"].Rows.Add(new object[] { 1, "苹果", 100 });
ds.Tables["stu"].Rows.Add(new object[] { 2, "香蕉", 100 });
ds.Tables["stu"].Rows.Add(new object[] { 3, "梨", 100 });
ds.Tables["stu"].Rows.Add(new object[] { 4, "哈密瓜", 100 });
ds.Tables["stu"].Rows.Add(new object[] { 5, "仙人果", 100 });
//绑定数据到ListBox控件
this.CheckBoxList1.DataSource = ds.Tables["stu"];
this.CheckBoxList1.DataValueField = "stuNo";
this.CheckBoxList1.DataTextField = "stuName";
this.CheckBoxList1.DataBind();
}
}
}
}