首先,我们需要从数据库中取到我们的数据
Class1:
1 string sqlcon = "Data Source=.;Initial Catalog=Test;User ID=sa;Password=******"; 2 3 public List<ModelClass> FindAll() 4 { 5 try 6 { 7 List<ModelClass> modList = new List<ModelClass>(); 8 using (SqlConnection con = new SqlConnection(sqlcon)) 9 { 10 con.Open(); 11 string sql = "select ID,Name,Address from TEST1"; 12 SqlCommand com = new SqlCommand(); 13 com.CommandText = sql; 14 com.CommandType = CommandType.Text; 15 com.Connection = con; 16 using (SqlDataReader read = com.ExecuteReader()) 17 { 18 while (read.Read()) 19 { 20 modList.Add(new ModelClass() { ID = Convert.ToInt32(read["ID"].ToString()), Name = read["Name"].ToString(), Address = read["Address"].ToString() }); 21 } 22 } 23 return modList; 24 } 25 } 26 catch (Exception) 27 { 28 throw; 29 } 30 } 31 32 public System.Data.DataSet FindAllToDS() 33 { 34 try 35 { 36 using (SqlConnection con = new SqlConnection(sqlcon)) 37 { 38 con.Open(); 39 System.Data.DataSet ds = new DataSet(); 40 string sql = "select ID,Name,Address from TEST1"; 41 SqlDataAdapter sda = new SqlDataAdapter(); 42 sda.SelectCommand = new SqlCommand(sql,con); 43 sda.Fill(ds); 44 return ds; 45 } 46 } 47 catch (Exception) 48 { 49 50 throw; 51 } 52 }
我分别用list和DataSet两种方式取得了数据,首先我们需要构造一个实现这样方法的类
gzClass:
1 public class gzClass 2 { 3 public int ID { get; set; } 4 public string NameAndAddress { get; set; } 5 }
第一种实现方法:在后台试用构造类去绑定数据
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 if (!IsPostBack) 4 { 5 //元数据 6 List<ModelClass> modelList = new List<ModelClass>(); 7 System.Data.DataTable dt = new System.Data.DataTable(); 8 //赋值护具 9 List<gzClass> strListToList = new List<gzClass>(); 10 List<gzClass> dtList = new List<gzClass>(); 11 //测试连接数据库类 12 Class1 c1 = new Class1(); 13 //赋值 14 modelList = c1.FindAll(); 15 dt = c1.FindAllToDS().Tables[0]; 16 //遍历取值并给与另一个构造类 17 foreach (ModelClass item in modelList) 18 { 19 strListToList.Add(new gzClass() { ID = item.ID, NameAndAddress = item.Name + "(" + item.Address + ")" }); 20 } 21 22 23 if (dt != null && dt.Rows.Count > 0) 24 { 25 foreach (System.Data.DataRow dsItem in dt.Rows) 26 { 27 dtList.Add(new gzClass() { ID = Convert.ToInt32(dsItem["ID"]), NameAndAddress = dsItem["Name"] + "(" + dsItem["Address"] + ")" }); 28 } 29 } 30 31 32 33 this.ddl_list.DataSource = strListToList; 34 this.ddl_list.DataTextField = "NameAndAddress"; 35 this.ddl_list.DataValueField = "ID"; 36 this.ddl_list.DataBind(); 37 38 this.ddl_ds.DataSource = dtList; 39 this.ddl_ds.DataTextField = "NameAndAddress"; 40 this.ddl_ds.DataValueField = "ID"; 41 this.ddl_ds.DataBind(); 42 43 } 44 }
第二种方法:使用ajax异步加载,进行拼接,这时就要借助我们的一般处理程序了
1 public void ProcessRequest(HttpContext context) 2 { 3 context.Response.ContentType = "text/plain"; 4 5 //元数据 6 List<ModelClass> modelList = new List<ModelClass>(); 7 System.Data.DataTable dt = new System.Data.DataTable(); 8 StringBuilder sbu1 = new StringBuilder(); 9 //测试连接数据库类 10 Class1 c1 = new Class1(); 11 //赋值 12 modelList = c1.FindAll(); 13 dt = c1.FindAllToDS().Tables[0]; 14 15 //拼接数据 16 sbu1.AppendLine("<select name="ddl_list" id="ddl_list">"); 17 foreach (ModelClass modelItem in modelList) 18 { 19 sbu1.AppendLine("<option value=""+modelItem.ID+"">"+modelItem.Name+"("+modelItem.Address+")</option>"); 20 } 21 sbu1.AppendLine("</select>"); 22 23 24 sbu1.AppendLine(" <select name="ddl_datarow" id="ddl_datarow">"); 25 foreach (System.Data.DataRow drItem in dt.Rows) 26 { 27 sbu1.AppendLine("<option value=""+drItem["ID"].ToString()+"">"+drItem["Name"]+"("+drItem["Address"]+")</option>"); 28 } 29 sbu1.AppendLine("</select>"); 30 context.Response.Write(sbu1.ToString()); 31 context.Response.End(); 32 }
把数据返回并在页面中给值,下面是我的aspx页面
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebTest1.WebForm1" %>
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 runat="server">
7 <title></title>
8 <script src="jquery-2.1.1.min.js" type="text/javascript"></script>
9 </head>
10 <body>
11 <form id="form1" runat="server">
12 <div>
13 <asp:DropDownList ID="ddl_list" runat="server">
14 </asp:DropDownList>
15 <asp:DropDownList ID="ddl_ds" runat="server">
16 </asp:DropDownList>
17 <div id="ddlDisplay"></div>
18 </div>
19 </form>
20 </body>
21 </html>
22 <script type="text/javascript">
23 window.onload = function () {
24 $.ajax({
25 url: "ddlTest1.ashx",
26 type: "post",
27 async: false,
28 success: function (data) {
29 if (data != "") {
30 $("#ddlDisplay").html(data);
31 }
32 }
33 });
34
35 }
36 </script>
本人想了半天只想出了这两种方法,欢迎补充,多多学习。