我们从启始页Default.aspx开始,微软的启始页当然是Default.aspx啦,该页面有一个用户控件NavigationControl 先来说说它,从简单入手嘛。
从它的Page_Load事件开始:
protected void Page_Load(object sender, EventArgs e)
{
GetControlStyle();//设置<td>的样式,在webconfig中已经配置了网站的全局样式App_Themes中有它的详细配置资料,在这里进行样式的选择
BindCategories();//这里对Repeater控件进行了绑定,请看BindCategories()方法;
//让它选择了连接会变颜色
string categoryId = Request.QueryString["categoryId"];
if (!string.IsNullOrEmpty(categoryId))
SelectCategory(categoryId);
//对网站采用缓存处理,这里可以查看,说的很清楚
DependencyAccess -> TableDependency -> IPetShopCacheDependency
this.CachePolicy.Dependency = DependencyFacade.GetCategoryDependency();
}
private void BindCategories() {
Category category = new Category();//这里调用BLL(业务逻辑层),对产品的类别进行处理
repCategories.DataSource = category.GetCategories();
//这个方法返回一个IList<CategoryInfo>它的类型为CategoryInfo位于Model项目中该项目有点像与数据库中相对应的表用来放数据便于对数据处理, 使用Ilist将许多CategoryInfo实例放进去
repCategories.DataBind();
//这样Repeater控件的绑定就完成了
}
private static readonly ICategory dal = PetShop.DALFactory.DataAccess.CreateCategory();
//数据抽象工厂不能继承此类,相对应的方法,这样做可以实现动态创建类和调用类型的功能:
public static PetShop.IDAL.ICategory CreateCategory() {
string className = path + ".Category";
return (PetShop.IDAL.ICategory)Assembly.Load(path).CreateInstance(className);
//这里返回的是SQLServerDAL.Category它继承了Icategory接口因此要实现GetCategories()与GetCategory()方法
}
//这里调用SQLServerDAL.Category.GetCategories()方法
public IList<CategoryInfo> GetCategories() {
return dal.GetCategories();
}
//SQLServerDAL.Category.GetCategories()方法
public IList<CategoryInfo> GetCategories() {
IList<CategoryInfo> categories = new List<CategoryInfo>();
//使用using对SqlDataReader自动释放
using(SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CATEGORIES, null)) {
while (rdr.Read()) {
CategoryInfo cat = new CategoryInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2));
categories.Add(cat);
}
}
return categories;
}