源代码:13033480群共享
一、数据访问层DAL
1、添加类库DAL,设置类库项目的程序集名称和默认命名空间;
2、在DAL中添加类Category.cs,设置类的属性为public;
3、在类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
1、在Web中新建文件夹Controls并在其中添加用户控件NavigationControl.ascx,窗体页和代码页代码分别如下:
<%@ 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>
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) { foreach (RepeaterItem item in repCategories.Items) { HiddenField hidCategoryId = (HiddenField)item.FindControl("hidCategoryId"); if(hidCategoryId.Value.ToLower() == categoryId.ToLower()) { HyperLink lnkCategory = (HyperLink)item.FindControl("lnkCategory"); lnkCategory.ForeColor = System.Drawing.Color.FromArgb(199, 116, 3); break; } } } private void BindCategories() { Category category = new Category(); repCategories.DataSource = category.GetCategories(); repCategories.DataBind(); } } }
2、注意在Web中添加引用→项目→Model,并在代码页文件中using WestGarden.Model;
三、删除原来的Default.aspx,重新建一个空的Default.aspx,然后,直接把用户控件拖入到页面中。