ASP.NET 数据绑定

1.生成数据库FreshLiveDB,附加或执行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类
新建商品类别数据访问层的类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
新建商品类别业务类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();
  }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。