网上购物系统(Task006)——用户界面层公共函数集WebUtility
频繁的操作,需要一个公共的数据库操作函数集(dbutility中的sqlhelper.cs);频繁的用户界面操作,也需要一个公共函数集webutility.cs。因为频繁,这个类及类中的函数,也做成了静态的。
一、app_code中添加类webutility.cs,并在类中添加函数getcategoryname()
[csharp] 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;
}
}
}
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的生命期。
[csharp] <add key="enablecaching" value="true"/>
<add key="categorycacheduration" value="12"/>
<add key="enablecaching" value="true"/>
<add key="categorycacheduration" value="12"/>
2、这个函数如果从数据库进行查询,需要调用dal中的getcategory()函数,为此,需要在category.cs中添加函数getcategory()
[csharp] 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;
}
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、窗体页代码
[html] <%@ 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="">
<head runat="server">
<title>the .net pet shop</title>
</head>
<body>
<form id="form1" runat="server">
<: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="" 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>
<%@ 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="">
<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">
<td align="right" style="padding-right: 5px; height: 32px;">
<a href="" 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、代码页代码
[csharp] 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);
}
}
}
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、应用母版代码:
[html] <%@ 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>
<%@ 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、设置窗体标题代码:
[csharp] 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"]);
}
}
}
作者 yousuosi