【求助】绑定DataTable到CheckBoxList
CheckBoxList的数据源可以用DataTable进行绑定吗?我写了测试用的绑定,但是绑定之后却没反应,好怪啊
是在找不到原因,大伙帮我看看
程序代码:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { InitData(); } private void InitData() { //链接数据库 SqlConnection myCon = new SqlConnection(); myCon.ConnectionString = "Persist Security Info=false; User id=sa;pwd=TianTang;database=northwind;server=(local)"; myCon.Open(); //使用sqlCommand提交查询命令 SqlCommand selectCMD = new SqlCommand("select * from Categories", myCon); //获取数据适配器 SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = selectCMD; //填充DataSet,得到DataTable DataSet ds = new DataSet(); da.Fill(ds); DataTable dt = ds.Tables["Categories"]; //将DataTable中的数据绑定到CheckBoxList CheckBoxList1.DataSource = dt; CheckBoxList1.DataTextField = "CategoryName"; CheckBoxList1.DataValueField = "CategoryID"; CheckBoxList1.DataBind(); //断开连接 myCon.Close(); } }
[[italic] 本帖最后由 天堂落日 于 2007-12-12 18:26 编辑 [/italic]]