源代码:13033480群共享
频繁的数据库操作,需要一个公共的数据库操作函数集(DBUtility中的SQLHelper.cs);频繁的用户界面操作,也需要一个公共函数集WebUtility.cs。因为频繁,这个类及类中的函数,也做成了静态的。
一、App_Code中添加类WebUtility.cs,并在类中添加函数GetCategoryName()
using System;
using System.Configuration;
using System.Web;
using System.Web.Caching;
using WestGarden.DAL;
namespace WestGarden.Web
{
public static class WebUtility
{
private const string CATEGORY_NAME_KEY = "category_name_{0}";
private static readonly bool enableCaching = bool.Parse(ConfigurationManager.AppSettings["EnableCaching"]);
public static string GetCategoryName(string categoryId)
{
Category category = new Category();
if (!enableCaching)
return category.GetCategory(categoryId).Name;
string cacheKey = string.Format(CATEGORY_NAME_KEY, categoryId);
string data = (string)HttpRuntime.Cache[cacheKey];
if (data == null)
{
int cacheDuration = int.Parse(ConfigurationManager.AppSettings["CategoryCacheDuration"]);
data = category.GetCategory(categoryId).Name;
HttpRuntime.Cache.Add(cacheKey, data, null, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
}
return data;
}
}
}
1、这个函数功能是获取类别名称,获取类别需要进行一下判断,如果允许Cache缓存,就从Cache中获取;如果不允许,就从数据库中查询。因些,使用这个函数需要在Web.config中添加两个设置,是否允许Cache以及Cache的生命期。
<add key="EnableCaching" value="true"/>
<add key="CategoryCacheDuration" value="12"/>
2、这个函数如果从数据库进行查询,需要调用DAL中的GetCategory()函数,为此,需要在Category.cs中添加函数GetCategory()
public CategoryInfo GetCategory(string categoryId)
{
CategoryInfo category = null;
SqlParameter parm = new SqlParameter(PARM_CATEGORY_ID, SqlDbType.VarChar, 20);
parm.Value = categoryId;
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CATEGORIES, parm))
{
if (rdr.Read())
category = new CategoryInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2));
else
category = new CategoryInfo();
}
return category;
}
二、Web添加母版MasterPage.master
1、窗体页代码
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="WestGarden.Web.MasterPage" %>
<%@ Register Src="Controls/NavigationControl.ascx" TagName="NavigationControl" TagPrefix="WestGardenControl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>The .NET Pet Shop</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="panFocus" runat="server" DefaultButton="btnSearch">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="780">
<tr valign="top">
<td rowspan="2">
<img src="Images/Comm_Images/Logo_home.jpg" alt="home" /></td>
<td class="homeBgSearch" height="25" width="141">
<asp:TextBox ID="txtSearch" runat="server" CssClass="homeSearchBox" Width="130px"></asp:TextBox></td>
<td class="homeBgSearch" width="50">
<asp:ImageButton ID="btnSearch" runat="server" AlternateText="Search" CausesValidation="false"
CssClass="paddingSearchicon" ImageUrl="Images/Comm_Images/button-search.gif" /></td>
<td class="homeBgSearch" width="50">
<asp:LoginStatus ID="lgnStatus" runat="server" CssClass="homeLink" LoginText="登 录"
LogoutAction="Redirect" LogoutPageUrl="~/Default.aspx" LogoutText="退 出" />
</td>
<td width="66">
</td>
</tr>
<tr>
<td colspan="3">
<img src="Images/Comm_Images/KFC.JPG" /></td>
<td width="66">
</td>
</tr>
</table>
</asp:Panel>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="780">
<tr>
<td colspan="4">
<img src="Images/Comm_Images/spacer.gif" height="5" /></td>
</tr>
<tr>
<td style="width: 10px">
</td>
<td width="105">
</td>
<td width="645">
</td>
<td width="20">
</td>
</tr>
<tr>
<td style="width: 10px">
</td>
<td>
</td>
<td class="pageHeader">
<asp:Literal ID="ltlHeader" runat="server"></asp:Literal>
</td>
<td>
</td>
</tr>
<tr>
<td style="width: 10px; height: 19px;">
</td>
<td style="height: 19px">
</td>
<td class="dottedLine" style="height: 19px">
</td>
<td style="height: 19px">
</td>
</tr>
<tr>
<td valign="top" style="width: 10px">
</td>
<td valign="top">
<WestGardenControl:NavigationControl ID="Categories" runat="server"></WestGardenControl:NavigationControl>
</td>
<td bgcolor="#FFFFFF" valign="top">
<asp:ContentPlaceHolder ID="cphPage" runat="server">
</asp:ContentPlaceHolder>
</td>
<td height="250">
</td>
</tr>
<tr>
<td style="width: 10px">
</td>
<td>
</td>
<td class="footer">
<table width="100%">
<tr>
<td style="height: 32px">
Version 1.0 版权所有:西园电脑工作室.QQ交流群:13033480</td>
<td align="right" style="padding-right: 5px; height: 32px;">
<a href="http://blog.csdn.com/yousuosi" target="_blank">
<img alt="西园软件制作" border="0" src="Images/Comm_Images/vertigo-icon.jpg" /></a></td>
</tr>
</table>
</td>
<td>
</td>
</tr>
</table>
</form>
</body>
</html>
2、代码页代码
using System;
using System.Web;
using System.Web.UI.WebControls;
namespace WestGarden.Web
{
public partial class MasterPage : System.Web.UI.MasterPage
{
private const string HEADER_PREFIX = "肯德基订餐系统——西园工作室 :: {0}";
protected void Page_PreRender(object sender, EventArgs e)
{
ltlHeader.Text = Page.Header.Title;
Page.Header.Title = string.Format(HEADER_PREFIX, Page.Header.Title);
}
}
}
三、为已建窗体Items.aspx应用母版,并在后台添加代码,设置窗体标题。
1、应用母版代码:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Items.aspx.cs" Inherits="WestGarden.Web.Items" %>
<%@ Register Src="Controls/ItemssControl.ascx" TagName="ItemsControl" TagPrefix="WestGardenControl" %>
<asp:Content ID="cntPage" ContentPlaceHolderID="cphPage" runat="Server" EnableViewState="false">
<WestGardenControl:ItemsControl ID="ItemsControl1" runat="server" />
</asp:Content>
2、设置窗体标题代码:
using WestGarden.DAL;
namespace WestGarden.Web
{
public partial class Items : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = WebUtility.GetCategoryName(Request.QueryString["categoryId"]);
}
}
}