网上购物系统(Task005)——自定义DateList控件分页显示商品信息
一、数据集Model添加商品信息类ItemInfo.cs。
[csharp] using System;
namespace WestGarden.Model
{
public class ItemInfo
{
private int itemid;
private string categoryid;
private string name;
private decimal price;
private string image;
private string categoryname;
public ItemInfo() { }
public ItemInfo(int itemid, string categoryid, string name, decimal price, string image, string categoryname)
{
this.itemid = itemid;
this.categoryid = categoryid;
this.name = name;
this.price = price;
this.image = image;
this.categoryname = categoryname;
}
public int ItemId
{
get { return itemid; }
set { itemid = value; }
}
public string CategoryId
{
get { return categoryid; }
set { categoryid = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public decimal Price
{
get { return price; }
set { price = value; }
}
public string Image
{
get { return image; }
set { image = value; }
}
public string CategoryName
{
get { return categoryname; }
set { categoryname = value; }
}
}
}
using System;
namespace WestGarden.Model
{
public class ItemInfo
{
private int itemid;
private string categoryid;
private string name;
private decimal price;
private string image;
private string categoryname;
public ItemInfo() { }
public ItemInfo(int itemid, string categoryid, string name, decimal price, string image, string categoryname)
{
this.itemid = itemid;
this.categoryid = categoryid;
this.name = name;
this.price = price;
this.image = image;
this.categoryname = categoryname;
}
public int ItemId
{
get { return itemid; }
set { itemid = value; }
}
public string CategoryId
{
get { return categoryid; }
set { categoryid = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public decimal Price
{
get { return price; }
set { price = value; }
}
public string Image
{
get { return image; }
set { image = value; }
}
public string CategoryName
{
get { return categoryname; }
set { categoryname = value; }
}
}
}
二、数据访问层DAL中添加类Item.cs,并添加函数GetItemsByCategory()
[csharp] using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using WestGarden.Model;
using WestGarden.DBUtility;
namespace WestGarden.DAL
{
public class Item
{
private const string SQL_SELECT_ITEMS_BY_CATEGORY = "SELECT Item.ItemId,Item.CategoryId,Item.Name,Item.Price,Item.Image,Category.Name FROM Item INNER JOIN Category ON Item.CategoryId=Category.CategoryId WHERE Item.CategoryId = @CategoryId";
private const string PARM_CATEGORY_ID = "@CategoryId";
public IList<ItemInfo> GetItemsByCategory(string CagegoryId)
{
IList<ItemInfo> itemsByCategory = new List<ItemInfo>();
SqlParameter parm = new SqlParameter(PARM_CATEGORY_ID, SqlDbType.VarChar, 20);
parm.Value = CagegoryId;
//Execute the query against the database
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_ITEMS_BY_CATEGORY, parm))
{
// Scroll through the results
while (rdr.Read())
{
ItemInfo item = new ItemInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetDecimal(3), rdr.GetString(4), rdr.GetString(5));
itemsByCategory.Add(item);
}
}
return itemsByCategory;
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using WestGarden.Model;
using WestGarden.DBUtility;
namespace WestGarden.DAL
{
public class Item
{
private const string SQL_SELECT_ITEMS_BY_CATEGORY = "SELECT Item.ItemId,Item.CategoryId,Item.Name,Item.Price,Item.Image,Category.Name FROM Item INNER JOIN Category ON Item.CategoryId=Category.CategoryId WHERE Item.CategoryId = @CategoryId";
private const string PARM_CATEGORY_ID = "@CategoryId";
public IList<ItemInfo> GetItemsByCategory(string CagegoryId)
{
IList<ItemInfo> itemsByCategory = new List<ItemInfo>();
SqlParameter parm = new SqlParameter(PARM_CATEGORY_ID, SqlDbType.VarChar, 20);
parm.Value = CagegoryId;
//Execute the query against the database
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_ITEMS_BY_CATEGORY, parm))
{
// Scroll through the results
while (rdr.Read())
{
ItemInfo item = new ItemInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetDecimal(3), rdr.GetString(4), rdr.GetString(5));
itemsByCategory.Add(item);
}
}
return itemsByCategory;
}
}
}
三、DataList显示商品信息1、 Item.aspx[html] <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Items1.aspx.cs" Inherits="WestGarden.Web.Items1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>肯德基订餐系统——西园工作室</title>
</head>
<body>
<form id="form1" runat="server">
<p>
<asp:DataList ID="dlstItems" runat="server">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:Image ID="imgItem" runat="server" AlternateText='<%# Eval("Name") %>' Height="120"
ImageUrl='<%# Eval("Image") %>' Width="120" />
</td>
</tr>
<tr>
<td>
<%# Eval("Name") %>
</td>
</tr>
<tr>
<td>
<%# Eval("Price") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList></p>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Items1.aspx.cs" Inherits="WestGarden.Web.Items1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>肯德基订餐系统——西园工作室</title>
</head>
<body>
<form id="form1" runat="server">
<p>
<asp:DataList ID="dlstItems" runat="server">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:Image ID="imgItem" runat="server" AlternateText='<%# Eval("Name") %>' Height="120"
ImageUrl='<%# Eval("Image") %>' Width="120" />
</td>
</tr>
<tr>
<td>
<%# Eval("Name") %>
</td>
</tr>
<tr>
<td>
<%# Eval("Price") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList></p>
</form>
</body>
</html>
2、Item.aspx.cx
namespace WestGarden.Web
{
public partial class Items1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string categoryKey = Request.QueryString["categoryId"];
Item item =new Item();
dlstItems.DataSource = item.GetItemsByCategory(categoryKey);
dlstItems.DataBind();
}
}
}
四、自定义DataList分页显示商品信息
1、自定义控件CustomList.cs,添加分页功能。
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WestGarden.Web {
public class CustomList : DataList {
//Static constants
protected const string HTML1 = "<table cellpadding=0 cellspacing=0><tr><td colspan=2>";
protected const string HTML2 = "</td></tr><tr><td class=paging align=left>";
protected const string HTML3 = "</td><td align=right class=paging>";
protected const string HTML4 = "</td></tr></table>";
private static readonly Regex RX = new Regex(@"^&page=\d+", RegexOptions.Compiled);
private const string LINK_PREV = "<a href=?page={0}>< 上一页</a>";
private const string LINK_MORE = "<a href=?page={0}>下一页 ></a>";
private const string KEY_PAGE = "page";
private const string COMMA = "?";
private const string AMP = "&";
protected string emptyText;
private IList dataSource;
private int pageSize = 10;
private int currentPageIndex;
private int itemCount;
override public object DataSource {
set {
//This try catch block is to avoid issues with the VS.NET designer
//The designer will try and bind a datasource which does not derive from ILIST
try {
dataSource = (IList)value;
ItemCount = dataSource.Count;
}
catch {
dataSource = null;
ItemCount = 0;
}
}
}
public int PageSize {
get { return pageSize; }
set { pageSize = value; }
}
protected int PageCount {
get { return (ItemCount - 1) / pageSize; }
}
virtual protected int ItemCount {
get { return itemCount; }
set { itemCount = value; }
}
virtual public int CurrentPageIndex {
get { return currentPageIndex; }
set { currentPageIndex = value; }
}
public string EmptyText {
set { emptyText = value; }
}
public void SetPage(int index) {
OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
}
override protected void OnLoad(EventArgs e) {
if (Visible) {
string page = Context.Request[KEY_PAGE];
int index = (page != null) ? int.Parse(page) : 0;
SetPage(index);
}
}
/// <summary>
/// Overriden method to control how the page is rendered
/// </summary>
/// <param name="writer"></param>
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));
//Close the table data tag
writer.Write(HTML3);
//Next button?
if (currentPageIndex < PageCount)
writer.Write(string.Format(LINK_MORE, (currentPageIndex + 1) + query));
//Close the table
writer.Write(HTML4);
}
override protected void OnDataBinding(EventArgs e) {
//Work out which items we want to render to the page
int start = CurrentPageIndex * pageSize;
int size = Math.Min(pageSize, ItemCount - start);
IList page = new ArrayList();
//Add the relevant items from the datasource
for (int i = 0; i < size; i++)
page.Add(dataSource[start + i]);
//set the base objects datasource
base.DataSource = page;
base.OnDataBinding(e);
}
public event DataGridPageChangedEventHandler PageIndexChanged;
virtual protected void OnPageIndexChanged(DataGridPageChangedEventArgs e) {
if (PageIndexChanged != null)
PageIndexChanged(this, e);
}
}
}
2、用户控件
1、ItemssControl.ascx
[html] <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Items1.aspx.cs" Inherits="WestGarden.Web.Items1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>肯德基订餐系统——西园工作室</title>
</head>
<body>
<form id="form1" runat="server">
<p>
<asp:DataList ID="dlstItems" runat="server">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:Image ID="imgItem" runat="server" AlternateText='<%# Eval("Name") %>' Height="120"
ImageUrl='<%# Eval("Image") %>' Width="120" />
</td>
</tr>
<tr>
<td>
<%# Eval("Name") %>
</td>
</tr>
<tr>
<td>
<%# Eval("Price") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList></p>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Items1.aspx.cs" Inherits="WestGarden.Web.Items1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>肯德基订餐系统——西园工作室</title>
</head>
<body>
<form id="form1" runat="server">
<p>
<asp:DataList ID="dlstItems" runat="server">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:Image ID="imgItem" runat="server" AlternateText='<%# Eval("Name") %>' Height="120"
ImageUrl='<%# Eval("Image") %>' Width="120" />
</td>
</tr>
<tr>
<td>
<%# Eval("Name") %>
</td>
</tr>
<tr>
<td>
<%# Eval("Price") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList></p>
</form>
</body>
</html>
2、ItemssControl.ascx.cs
[csharp] using System;
using System.Web;
using System.Web.UI.WebControls;
using WestGarden.DAL;
namespace WestGarden.Web {
public partial class ItemsControl : System.Web.UI.UserControl {
/// <summary>
/// Rebind control
/// </summary>
protected void PageChanged(object sender, DataGridPageChangedEventArgs e) {
//reset index
dlstItems.CurrentPageIndex = e.NewPageIndex;
//get category id
string categoryKey = Request.QueryString["categoryId"];
//bind data
Item item = new Item();
dlstItems.DataSource = item.GetItemsByCategory(categoryKey);
dlstItems.DataBind();
}
}
}
作者 yousuosi