四大列表控件之CheckBoxList控件

Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
    <title>无标题页</title> </head> <body>
    <form id="form1" runat="server">
    <div>
    
    可供选择的水果:
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" 
        Width="180px">
    </asp:CheckBoxList></div>
    </form> </body> </html>

绑定数据到ListBox控件

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();
            }

        }
    }

结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43126276/article/details/84949850