商品搜索结果的代码实现:
Product类,方法SearchProducts():
public static SqlDataReader SearchProducts (string keyword, int pageSize, int pageIndex)
{ SqlParameter[] para = { new SqlParameter("@KeyWord", keyword),
new SqlParameter("@pageSize", pageSize),
new SqlParameter("@pageIndex", pageIndex)
};
return SQLHelper.ExecuteReader(SQLHelper.CONN_STRING,
CommandType.StoredProcedure, "SearchProducts", para);
}
(Search.aspx.cs)代码如下:
private static int PageSize = 10;
private void Page_Load(object sender, System.EventArgs e)
{ if (!Page.IsPostBack)
{ if (Request.QueryString["keywords"]!="")
{ ShowResult(0, PageSize);
}
}
}
void ShowResult(int pageIndex, int pageSize)
{ products.DataSource = BLL.Product.SearchProducts(Request.QueryString["keywords"], pageSize, pageIndex);
products.DataBind();
int resultCount = BLL.Product.GetSearchResultCount(Request.QueryString["keywords"]);
int count;
if (resultCount%PageSize == 0)
{ count = resultCount/PageSize;
PageCount.Text = count.ToString();
}
else
{ count = resultCount/PageSize+1;
PageCount.Text = count.ToString();
}
page.Items.Clear();
for(int i=0; i<count; i++)
{ ListItem item = new ListItem((i+1).ToString(), i.ToString());
page.Items.Add(item);
}
page.SelectedIndex = pageIndex;
}
private void page_SelectedIndexChanged(object sender, System.EventArgs e)
{ ShowResult(page.SelectedIndex, PageSize);
}
3、购物车功能模块的实现:
这一模块主要包括:添加到购物车和购物车管理,其代码如下:
添加到购物车的代码实现:
ShoppingCart类,方法GetShoppingCartId():
public String GetShoppingCartId()
{ // 获得HTTP请求的HttpContext实例
System.Web.HttpContext context = System.Web.HttpContext.Current;
// 如果用户已通过验证,则返回其用户编号作为CartId
if (context.User.Identity.Name != "")
{ return context.User.Identity.Name;
}
// 如果请求用户为匿名,则创建临时的CartId
if (context.Request.Cookies["eshop_CartID"] != null)
{ return context.Request.Cookies["eshop_CartID"].Value;
}
else
{ // 生成GUID
Guid tempCartId = Guid.NewGuid();
// 将GUID保存到Cookie
context.Response.Cookies["eshop_CartID"].Value = tempCartId.ToString();
// 返回临时CartId
return tempCartId.ToString();
}
}
ShoppingCart类,方法AddItem():
public void AddItem(string cartId, int productId, int quantity)
{ SqlParameter[] para = { new SqlParameter("@cartId", cartId),
new SqlParameter("@productId", productId),
new SqlParameter("@quantity", quantity)
};
DAL.SQLHelper.ExecuteNonQuery(DAL.SQLHelper.CONN_STRING, CommandType.StoredProcedure,
"ShoppingCartAddItem", para);
}
(AddToCart.aspx.cs)代码如下:
private void Page_Load(object sender, System.EventArgs e)
{ if (Request["productId"] != null)
{ BLL.ShoppingCart cart = new eshop.BLL.ShoppingCart();
//获取CartId
string cartId = cart.GetShoppingCartId();
//向购物车添加某种商品,数量为1
cart.AddItem(cartId, int.Parse(Request["productId"]), 1);
}
//跳转到购物车页面
Response.Redirect("ShoppingCart.aspx");
}
购物车管理的代码实现:
(ShoppingCart.aspx.cs)代码实现:
private void Page_Load(object sender, System.EventArgs e)
{ if (!Page.IsPostBack)
{ ShowShoppingCartList();
ShowMenu();
}
}
void ShowMenu()
{ if (Request.IsAuthenticated == true)
{ LogOutArea.Visible = false;
LogInArea.Visible = true;
}
else
{ LogOutArea.Visible = true;
LogInArea.Visible = false;
}
}
void ShowShoppingCartList()
{ BLL.ShoppingCart cart = new BLL.ShoppingCart();
// 得到用户的购物车ID
String cartID = cart.GetShoppingCartId();
// 如果购物车内没有商品,DataGrid隐藏
if (cart.GetItemCount(cartID) == 0)
{ DetailsPanel.Visible = false;
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] ... 下一页 >>