网上购物系统(Task004)——数据访问层DAL
一、添加类库DAL,在DAL中添加类Category.cs,并在类库中添加函数GetCategories(),代码如下:
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using WestGarden.Model;
using WestGarden.DBUtility;
namespace WestGarden.DAL
{
public class Category
{
// Static constants
private const string SQL_SELECT_CATEGORIES = "SELECT CategoryId, Name, Descn FROM Category";
/// <summary>
/// Method to get all categories
/// </summary>
public IList<CategoryInfo> GetCategories()
{
IList<CategoryInfo> categories = new List<CategoryInfo>();
//Execute a query to read the categories
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;
}
}
}
函数GetCategories()要在SQLHelper中读取连接字符串,因此,需要在SQLHelper.cs中添加代码:
public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["NetShopConnString"].ConnectionString;
注意添加引用System.Configuration。
二、使用用户控件
在Web中新建文件夹Controls并在其中添加用户控件NavigationControl.ascx,窗体页和代码页代码分别如下:
[html] <%@ Control Language="C#" AutoEventWireup="true" CodeFile="NavigationControl.ascx.cs" Inherits="WestGarden.Web.NavigationControl" %>
<%@ OutputCache Duration="100000" VaryByParam="*" %>
<asp:Repeater ID="repCategories" runat="server">
<HeaderTemplate>
<table cellspacing="0" border="0" style="border-collapse: collapse;">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="<%= ControlStyle %>"><asp:HyperLink runat="server" ID="lnkCategory" NavigateUrl='<%# string.Format("~/Items.aspx?page=0&categoryId={0}", Eval("CategoryId")) %>' Text='<%# Eval("Name") %>' /><asp:HiddenField runat="server" ID="hidCategoryId" Value='<%# Eval("CategoryId") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="NavigationControl.ascx.cs" Inherits="WestGarden.Web.NavigationControl" %>
<%@ OutputCache Duration="100000" VaryByParam="*" %>
<asp:Repeater ID="repCategories" runat="server">
<HeaderTemplate>
<table cellspacing="0" border="0" style="border-collapse: collapse;">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="<%= ControlStyle %>"><asp:HyperLink runat="server" ID="lnkCategory" NavigateUrl='<%# string.Format("~/Items.aspx?page=0&categoryId={0}", Eval("CategoryId")) %>' Text='<%# Eval("Name") %>' /><asp:HiddenField runat="server" ID="hidCategoryId" Value='<%# Eval("CategoryId") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
[csharp] using System;
using System.Web.UI.WebControls;
using WestGarden.DAL;
namespace WestGarden.Web {
public partial class NavigationControl : System.Web.UI.UserControl {
private string controlStyle;
protected string ControlStyle {
get { return controlStyle; }
}
protected void GetControlStyle() {
if (Request.ServerVariables["SCRIPT_NAME"].ToLower().IndexOf("default.aspx") > 0)
controlStyle = "navigationLinks";
else
controlStyle = "mainNavigation";
}
protected void Page_Load(object sender, EventArgs e) {
GetControlStyle();
BindCategories();
string categoryId = Request.QueryString["categoryId"];
if (!string.IsNullOrEmpty(categoryId))
SelectCategory(categoryId);
}
private void SelectCategory(string categoryId) {
&nb
补充:Web开发 , ASP.Net ,