asp.net 列表控件

web空间类都被放置在System.Web.UI.WebControls命名空间下
1、ListBox
  ListBox控件用于创建多选的下拉列表,而可选项是通过ListItem元素来定义的。
示例代码如下:

 1  protected void Page_Load(object sender, EventArgs e)
 2     {
 3         if(!Page.IsPostBack)
 4         {
 5         DataSet ds = new DataSet();
 6         ds.Tables.Add("stu");
 7         ds.Tables["stu"].Columns.Add("stuNo",typeof(int));
 8         ds.Tables["stu"].Columns.Add("stuName", typeof(string));
 9         ds.Tables["stu"].Columns.Add("stuScore", typeof(int));
10         ds.Tables["stu"].Rows.Add(new object[]{1,"张一",100});
11         ds.Tables["stu"].Rows.Add(new object[] { 2, "王二", 100 });
12         ds.Tables["stu"].Rows.Add(new object[] { 3, "刘三", 100 });
13         ds.Tables["stu"].Rows.Add(new object[] { 4, "赵四", 100 });
14         ds.Tables["stu"].Rows.Add(new object[] { 5, "李五", 100 });
15         ListBox1.DataSource=ds.Tables["stu"];
16         ListBox1.DataValueField = "stuNo";
17         ListBox1.DataTextField = "stuName";
18         ListBox1.DataBind();
19         }
20     }
View Code

2、DropWownList
   实例代码如下:

 1  protected void Page_Load(object sender, EventArgs e)
 2     {
 3         if(!Page.IsPostBack)
 4         {
 5         DataSet ds = new DataSet();
 6         ds.Tables.Add("stu");
 7         ds.Tables["stu"].Columns.Add("stuNo",typeof(int));
 8         ds.Tables["stu"].Columns.Add("stuName", typeof(string));
 9         ds.Tables["stu"].Columns.Add("stuScore", typeof(int));
10         ds.Tables["stu"].Rows.Add(new object[]{1,"张一",100});
11         ds.Tables["stu"].Rows.Add(new object[] { 2, "王二", 100 });
12         ds.Tables["stu"].Rows.Add(new object[] { 3, "刘三", 100 });
13         ds.Tables["stu"].Rows.Add(new object[] { 4, "赵四", 100 });
14         ds.Tables["stu"].Rows.Add(new object[] { 5, "李五", 100 });
15         DropDownList1.DataSource=ds.Tables["stu"];
16         DropDownList1.DataValueField = "stuNo";
17         DropDownList1.DataTextField = "stuName";
18         DropDownList1.DataBind();
19         }
20     }
View Code

3、CheckBoxList
CheckBoxList控件用来创建多项选择的复选框,该复选框组可以通过将控件绑定到数据源动态创建。
实例代码如下:

 1     protected void Page_Load(object sender, EventArgs e)
 2     {
 3         if(!Page.IsPostBack)
 4         {
 5         DataSet ds = new DataSet();
 6         ds.Tables.Add("stu");
 7         ds.Tables["stu"].Columns.Add("stuNo",typeof(int));
 8         ds.Tables["stu"].Columns.Add("stuName", typeof(string));
 9         ds.Tables["stu"].Columns.Add("stuScore", typeof(int));
10         ds.Tables["stu"].Rows.Add(new object[]{1,"张一",100});
11         ds.Tables["stu"].Rows.Add(new object[] { 2, "王二", 100 });
12         ds.Tables["stu"].Rows.Add(new object[] { 3, "刘三", 100 });
13         ds.Tables["stu"].Rows.Add(new object[] { 4, "赵四", 100 });
14         ds.Tables["stu"].Rows.Add(new object[] { 5, "李五", 100 });
15        CheckBoxList1.DataSource=ds.Tables["stu"];
16        CheckBoxList1.DataValueField = "stuNo";
17        CheckBoxList1.DataTextField = "stuName";
18        CheckBoxList1.DataBind();
19         }
20     }
View Code

4、RadioButtonList
该控件为网页开发人员提供了一组单选按钮,这些按钮可以通过绑定动态生成。
示例代码如下:

 1   protected void Page_Load(object sender, EventArgs e)
 2     {
 3         if(!Page.IsPostBack)
 4         {
 5         DataSet ds = new DataSet();
 6         ds.Tables.Add("stu");
 7         ds.Tables["stu"].Columns.Add("stuNo",typeof(int));
 8         ds.Tables["stu"].Columns.Add("stuName", typeof(string));
 9         ds.Tables["stu"].Columns.Add("stuScore", typeof(int));
10         ds.Tables["stu"].Rows.Add(new object[]{1,"张一",100});
11         ds.Tables["stu"].Rows.Add(new object[] { 2, "王二", 100 });
12         ds.Tables["stu"].Rows.Add(new object[] { 3, "刘三", 100 });
13         ds.Tables["stu"].Rows.Add(new object[] { 4, "赵四", 100 });
14         ds.Tables["stu"].Rows.Add(new object[] { 5, "李五", 100 });
15       RadioButtonList1.DataSource=ds.Tables["stu"];
16       RadioButtonList1.DataValueField = "stuNo";
17       RadioButtonList1.DataTextField = "stuName";
18       RadioButtonList1.DataBind();
19         }
20     }
View Code

5、BulletedList
该控件用来创建一个采用项目符号格式的项列表,可以动态绑定生成
实例代码如下:

 1  protected void Page_Load(object sender, EventArgs e)
 2     {
 3         BulletedList1.BulletStyle = BulletStyle.Circle;
 4         if(!Page.IsPostBack)
 5         {
 6         DataSet ds = new DataSet();
 7         ds.Tables.Add("stu");
 8         ds.Tables["stu"].Columns.Add("stuNo",typeof(int));
 9         ds.Tables["stu"].Columns.Add("stuName", typeof(string));
10         ds.Tables["stu"].Columns.Add("stuScore", typeof(int));
11         ds.Tables["stu"].Rows.Add(new object[]{1,"张一",100});
12         ds.Tables["stu"].Rows.Add(new object[] { 2, "王二", 100 });
13         ds.Tables["stu"].Rows.Add(new object[] { 3, "刘三", 100 });
14         ds.Tables["stu"].Rows.Add(new object[] { 4, "赵四", 100 });
15         ds.Tables["stu"].Rows.Add(new object[] { 5, "李五", 100 });
16         BulletedList1.DataSource = ds.Tables["stu"];
17         BulletedList1.DataValueField = "stuNo";
18         BulletedList1.DataTextField = "stuName";
19         BulletedList1.DataBind();
20         }
21     }
View Code
原文地址:https://www.cnblogs.com/net064/p/5673341.html