ASP.NET 數(shù)據(jù)綁定

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();
  }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容