学习PetShop3.0(2)宠物展示[1]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

宠物展示,也就是商品展示(汗……)
涉及到这个主题的页有Category.aspx / Items.aspx / ItemDetails.aspx,分别是大类/小类/详细信息这三个。下面来一个一个的分析
要注意的是,像第一篇说的,数据的传递都是采用直接传递业务实体的方法来完成。这样是不是有很强的面向对象的味道?
Category.aspx
该页面的主体是一个用户自定义控件:SimplePager,该空间继承自Repeater,作用当然是存放宠物信息,他的数据源的获得方式使用了.net的缓存api。具体实现代码为:
if(Cache[categoryKey] != null){
// If the data is already cached, then used the cached copy
products.DataSource = (IList)Cache[categoryKey];
}else{
// If the data is not cached, then create a new products object and request the data
Product product = new Product();
IList productsByCategory = product.GetProductsByCategory(categoryKey);
// Store the results of the call in the Cache and set the time out to 12 hours
Cache.Add(categoryKey, productsByCategory, null, DateTime.Now.AddHours(12), Cache.NoSlidingExpiration , CacheItemPriority.High, null);
products.DataSource = productsByCategory;
}
// Bind the data to the control
products.DataBind();
是使用了很普遍的做法来完成的,新数据的通过PetShop.BLL.Product.GetProductsByCategory方法获得,梢后分析这个方法。SimplePager获得数据后,就像普通的Repeater一样根据模板的设定来显示数据。对SimplePager的分析同样放在后面。
用户点击具体的小类后,就进入小类的页面,通过url传递小类的编号。
Items.aspx
和上面的页面基本一样,因为都是对类别进行的操作嘛。
选择具体的宠物后,就进入详细资料展示页面了。
ItemDetails.aspx
这个页面没有使用缓存,而是直接通过业务逻辑层到数据库去取记录,然后返回到表示层。很简单,一看就懂的那种。
下面来看一下SimplePager控件。
控件的定义中重写了很多Repeater的方法,其用意主要是为了是其具有分页的功能,看一下其中的Render方法。
PetShop.Web.Controls.SimplePager : Repeater
override protected void Render(HtmlTextWriter writer) {

//Check there is some data attached

if (ItemCount == 0) {
writer.Write(emptyText);
return;
}

//Mask the query
string query = Context.Request.Url.Query.Replace(COMMA, AMP);
query = RX.Replace(query, string.Empty);

// Write out the first part of the control, the table header
writer.Write(HTML1);

// Call the inherited method
base.Render(writer);

//从这里往下开始是重写的关键部分,用来显示上下页的按钮
// Write out a table row closure

writer.Write(HTML2);

//Determin whether next and previous buttons are required
//Previous button?

if (currentPageIndex > 0)
writer.Write(string.Format(LINK_PREV, (currentPageIndex - 1) + query));

//other .....


}

另外该控件还有自定义的事件PageIndexChanged,用来对页面切换进行响应。DataSource接受实现IList接口的数据,而我们看上面大类页面在显示的时候使用的语句
products.DataSource = productsByCategory;
productsByCategory作为业务逻辑组件一个方法的返回值,正是实现了IList接口的数据。
接下来看该数据是怎么得到的。
基本的过程都是从最后的数据操作组件开始层层的传递过来,因此我们直接看最后的数据操作部分。
PetShop.SQLServerDAL.Product : IProduct
public IList GetProductsByCategory(string category) {

IList productsByCategory = new ArrayList();

本文关键:学习PetShop3.0(2)宠物展示
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top