1.生成數(shù)據(jù)庫FreshLiveDB,附加或執(zhí)行sql腳本
2.新建ASP.NET WEB窗體應用程序
3.新建項目-類庫Models
新建類ProductClass
public class ProductClass
{
public int ClassID { get; set; }
public string ClassName { get; set; }
public int ParentClassID { get; set; }
}
4.新建項目-類庫DAL,添加引用-類庫Models
加入DBHelper.cs類
新建商品類別數(shù)據(jù)訪問層的類ProductClassService
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace DAL
{
public class ProductClassService
{
public static List<ProductClass> SelectByID(int parentID)
{
List<ProductClass> list = new List<ProductClass>();
string strSql = "select classID, className, ParentClassID from productClass where parentclassID=" + parentID;
DataTable dt = DBHelper.Instance().GetDataTableBySql(strSql);
if(dt != null)
{
foreach (DataRow dr in dt.Rows)
{
ProductClass pc = new ProductClass();
pc.ClassID = int.Parse(dr["classID"].ToString());
pc.ClassName = dr["className"].ToString();
pc.ParentClassID = int.Parse(dr["ParentClassID"].ToString());
list.Add(pc);
}
}
return list;
}
}
}
5.新建項目-類庫BLL,添加引用-類庫Models和DAL
新建商品類別業(yè)務類ProductClassManager
using DAL;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BLL
{
public class ProductClassManager
{
public static List<ProductClass> SelectList()
{
return ProductClassService.SelectByID(0);
}
}
}
6.頁面層加入引用-類庫BBL和類庫Models
新建頁面ProductList.aspx,頁面中加入控件
6.1頁面控件代碼
<asp:DropDownList ID="ddlProductClass" runat="server">
</asp:DropDownList>
6.2在ProductList.aspx頁面的后端類中編寫如下代碼:
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
BindProductClass();
}
}
void BindProductClass()
{
this.ddlProductClass.DataSource = ProductClassManager.SelectList();
this.ddlProductClass.DataTextField = "ClassName";
this.ddlProductClass.DataValueField = "ClassID";
this.ddlProductClass.DataBind();
}