前台代码:
绑定DataTabel到CheckBoxList<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server" >
</asp:CheckBoxList></div>
后台cs代码:
public partial class test_DropDownList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CheckListBind();
}
public void CheckListBind()
{
SqlConnection mycon = new SqlConnection("Persist Security Info=False;User id=123;pwd=123;database=Northwind;server=(local)");
mycon.Open();
SqlCommand smd = new SqlCommand("SELECT CategoryName,CategoryID FROM Categories",mycon);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = smd;
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables["Categories"];
CheckBoxList1.DataSource = dt;
CheckBoxList1.DataTextField = "CategoryName";
CheckBoxList1.DataValueField = "CategoryID";
CheckBoxList1.DataBind();
mycon.Close();
}
}
运行时显示不出任何东西,也没报错,请教高手指点.