业务逻辑>表现层>列表控件 数据选择>手动绑定到数据源

首先是前台页面:

 

  1  <% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " CH9_DemoForm003.aspx.cs "  Inherits = " CH9_DemoForm003 "   %>
  2 
  3  <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
  4 
  5  < html  xmlns ="http://www.w3.org/1999/xhtml" >
  6  < head  id ="Head1"  runat ="server" >
  7       < title > 示范以程序控制方式绑定列表类型控件 </ title >
  8       < style  type ="text/css" >
  9          #form1
 10           {
 11              text-align :  center ;
 12           }
 13          body
 14           {
 15              font-family :  Lucida Sans Unicode ;
 16              font-size :  10pt ;
 17           }
 18          button
 19           {
 20              font-family :  tahoma ;
 21              font-size :  8pt ;
 22           }
 23          .highlight
 24           {
 25              display :  block ;
 26              color :  red ;
 27              font :  bold 24px Arial ;
 28              margin :  10px ;
 29           }
 30          .style1
 31           {
 32              width :  312px ;
 33              text-align :  left ;
 34           }
 35       </ style >
 36  </ head >
 37  < body >
 38       < form  id ="form1"  runat ="server" >
 39       < div >
 40         < table  border ="1" >
 41               < tr >
 42                   < td  colspan ="2" >
 43                      Hi
 44                       < b > <% = Request.LogonUserIdentity.Name %> </ b >
 45                      您好,您的 IP 地址是
 46                       < b > <% = Request.UserHostAddress %> </ b >
 47                       < br  /> 欢迎光临并请选择下列选项:
 48                   </ td >
 49               </ tr >
 50               < tr >
 51                   < td  style ="text-align: right" >
 52                      请选择您的性别:
 53                   </ td >
 54                   < td  class ="style1" >
 55                       < asp:RadioButtonList  ID ="Gender_RadioButtonList"  runat ="server" >
 56                           < asp:ListItem  Selected ="True"  Value ="0" > </ asp:ListItem >
 57                           < asp:ListItem  Value ="1" > </ asp:ListItem >
 58                       </ asp:RadioButtonList >
 59                   </ td >
 60               </ tr >
 61               < tr >
 62                   < td  style ="text-align: right" >
 63                      请问您已婚或未婚:
 64                   </ td >
 65                   < td  class ="style1" >
 66                       < asp:ListBox  ID ="Marital_ListBox"  runat ="server"  Height ="45px" >
 67                           < asp:ListItem  Selected ="True"  Value ="0" > 未婚 </ asp:ListItem >
 68                           < asp:ListItem  Value ="1" > 已婚 </ asp:ListItem >
 69                       </ asp:ListBox >
 70                   </ td >
 71               </ tr >
 72               < tr >
 73                   < td  style ="text-align: right" >
 74                      请选择居住地:
 75                   </ td >
 76                   < td  class ="style1" >
 77                       < asp:DropDownList  ID ="Resident_DropDownList"  runat ="server"  Height ="22px"  
 78                          Width ="123px" >
 79                       </ asp:DropDownList >
 80                   </ td >
 81               </ tr >
 82               < tr >
 83                   < td  style ="text-align: right" >
 84                      请选择您的职业:
 85                   </ td >
 86                   < td  class ="style1" >
 87                       < asp:DropDownList  ID ="Job_DropDownList"  runat ="server"  
 88                          Height ="22px"  Width ="278px" >
 89                       </ asp:DropDownList >
 90                   </ td >
 91               </ tr >             
 92               < tr >
 93                   < td  style ="text-align: right" >
 94                      请选择您的专长:
 95                   </ td >
 96                   < td  class ="style1" >
 97                       < asp:DropDownList  ID ="Speciality_DropDownList"  runat ="server"  
 98                          Height ="22px"  Width ="278px" >
 99                       </ asp:DropDownList >
100                   </ td >
101               </ tr >                         
102           </ table >        
103       </ div >
104       </ form >
105  </ body >
106  </ html >
107 
108 

 

 

Job_DropDownListSpeciality_DropDownList是没有用数据源控件绑定,而是后台代码手动绑定到数据源。

 

后台代码:

 

 1  using  System;
 2  using  System.Collections;
 3  using  System.Configuration;
 4  using  System.Data;
 5  using  System.Linq;
 6  using  System.Web;
 7  using  System.Web.Security;
 8  using  System.Web.UI;
 9  using  System.Web.UI.HtmlControls;
10  using  System.Web.UI.WebControls;
11  using  System.Web.UI.WebControls.WebParts;
12  using  System.Xml.Linq;
13  using  System.Data.SqlClient;
14  using  System.Collections.Generic;
15 
16  public   partial   class  CH9_DemoForm003 : System.Web.UI.Page
17  {
18       protected   void  Page_Load( object  sender, EventArgs e)
19      {
20           if  ( ! IsPostBack)
21          {
22               //  创建一个连接对象。
23               using  (SqlConnection con  =   new  SqlConnection(ConfigurationManager.ConnectionStrings[ " chtNorthwind " ].ConnectionString))
24              {
25 
26                   //  创建一个命令对象。
27                  SqlCommand cmd  =   new  SqlCommand();
28                  cmd.Connection  =  con;
29                  cmd.CommandText  =   " SELECT 县市代号, 县市名称 FROM 县市 " ;
30 
31                   //  开启连接。
32                  con.Open();
33 
34                   //  创建一个 SqlDataReader 对象。
35                   using  (SqlDataReader dr  =  cmd.ExecuteReader(CommandBehavior.SingleResult))
36                  {
37                       //  将 DropDownList 控件绑定至 SqlDataReader 对象。                    
38                       this .Resident_DropDownList.DataSource  =  dr;
39                       this .Resident_DropDownList.DataTextField  =   " 县市名称 " ;
40                       this .Resident_DropDownList.DataValueField  =   " 县市代号 " ;
41                       this .Resident_DropDownList.DataBind();
42                  }
43 
44                  cmd.CommandText  =   " SELECT 职业类别名称, 职业类别编号 FROM 职业类别 " ;
45                   using  (SqlDataReader dr  =  cmd.ExecuteReader(CommandBehavior.SingleResult))
46                  {
47                       //  将 DropDownList 控件绑定至 SqlDataReader 对象。
48                       this .Job_DropDownList.DataSource  =  dr;
49                       this .Job_DropDownList.DataTextField  =   " 职业类别名称 " ;
50                       this .Job_DropDownList.DataValueField  =   " 职业类别编号 " ;
51                       this .Job_DropDownList.DataBind();
52                  }
53              }
54 
55               //  创建一个泛型集合来内含 Speciality 对象。
56              List < Speciality >  shoppingCart  =   new  List < Speciality > ();
57              shoppingCart.Add( new  Speciality( 1 " 数据库设计 " ));
58              shoppingCart.Add( new  Speciality( 2 " 市场分析 " ));
59              shoppingCart.Add( new  Speciality( 3 " 交际 " ));
60              shoppingCart.Add( new  Speciality( 4 " 决策分析 " ));
61 
62               //  将 DropDownList 控件绑定至泛型集合。
63              Speciality_DropDownList.DataSource  =  shoppingCart;
64              Speciality_DropDownList.DataTextField  =   " Name " ;
65              Speciality_DropDownList.DataValueField  =   " Id " ;
66              Speciality_DropDownList.DataBind();
67 
68          }
69 
70      }
71  }
72 
73  public   class  Speciality
74  {
75       private   int  _id;
76       public   string  _name;
77       public   int  Id
78      {
79           get  {  return  _id; }
80      }
81 
82       public   string  Name
83      {
84           get  {  return  _name; }
85      }
86 
87       public  Speciality( int  id,  string  name)
88      {
89          _id  =  id;
90          _name  =  name;
91      }
92  }
93 
94 
95 

 

对于DropDownList 有两种后台绑定方式,一种数据库,另一种泛型集合。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值