頻繁的數據庫操作
需要一個公共的數據庫操作函數集(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_{
}
;
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_{
}
;
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;
}
}
}
這個函數功能是獲取類別名稱
獲取類別需要進行一下判斷
如果允許Cache緩存
就從Cache中獲取
如果不允許
就從數據庫中查詢
因些
使用這個函數需要在nfig中添加兩個設置
是否允許Cache以及Cache的生命期
[csharp] <add key=
EnableCaching
value=
true
/>
<add key=
CategoryCacheDuration
value=
/>
<add key=
EnableCaching
value=
true
/>
<add key=
CategoryCacheDuration
value=
/>
這個函數如果從數據庫進行查詢
需要調用DAL中的GetCategory()函數
為此
需要在Category
cs中添加函數GetCategory()
[csharp] public CategoryInfo GetCategory(string categoryId)
{
CategoryInfo category = null;
SqlParameter parm = new SqlParameter(Parm_CATEGORY_ID
SqlDbType
VarChar
)
parm
Value = categoryId;
using (SqlDataReader rdr = SqlHelper
ExecuteReader(SqlHelper
ConnectionStringLocalTransaction
CommandType
Text
SQL_SELECT_CATEGORIES
parm))
{
if (rdr
Read())
category = new CategoryInfo(rdr
GetString(
)
rdr
GetString(
)
rdr
GetString(
))
else
category = new CategoryInfo()
}
return category;
}
public CategoryInfo GetCategory(string categoryId)
{
CategoryInfo category = null;
SqlParameter parm = new SqlParameter(Parm_CATEGORY_ID
SqlDbType
VarChar
)
parm
Value = categoryId;
using (SqlDataReader rdr = SqlHelper
ExecuteReader(SqlHelper
ConnectionStringLocalTransaction
CommandType
Text
SQL_SELECT_CATEGORIES
parm))
{
if (rdr
Read())
category = new CategoryInfo(rdr
GetString(
)
rdr
GetString(
)
rdr
GetString(
))
else
category = new CategoryInfo()
}
return category;
}
二
Web添加母版MasterPage
master
窗體頁代碼
[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
//W
C//DTD XHTML
//EN
>
<html xmlns=
>
<head runat=
server
>
<title>The
NET Pet Shop</title>
</head>
<body>
<form id=
form
runat=
server
>
<asp:Panel ID=
panFocus
runat=
server
DefaultButton=
btnSearch
>
<table align=
center
border=
cellpadding=
cellspacing=
width=
>
<tr valign=
top
>
<td rowspan=
>
<img src=
Images/Comm_Images/Logo_home
jpg
alt=
home
/></td>
<td class=
homeBgSearch
height=
width=
>
<asp:TextBox ID=
txtSearch
runat=
server
CssClass=
homeSearchBox
Width=
px
></asp:TextBox></td>
<td class=
homeBgSearch
width=
>
<asp:ImageButton ID=
btnSearch
runat=
server
AlternateText=
Search
CausesValidation=
false
CssClass=
paddingSearchicon
ImageUrl=
Images/Comm_Images/button
search
gif
/></td>
<td class=
homeBgSearch
width=
>
<asp:LoginStatus ID=
lgnStatus
runat=
server
CssClass=
homeLink
LoginText=
登 錄
LogoutAction=
Redirect
LogoutPageUrl=
~/Default
aspx
LogoutText=
退 出
/>
</td>
<td width=
>
</td>
</tr>
<tr>
<td colspan=
>
<img src=
Images/Comm_Images/KFC
JPG
/></td>
<td width=
>
</td>
</tr>
</table>
</asp:Panel>
<table align=
center
border=
cellpadding=
cellspacing=
width=
>
<tr>
<td colspan=
>
<img src=
Images/Comm_Images/spacer
gif
height=
/></td>
</tr>
<tr>
<td >
</td>
<td width=
>
</td>
<td width=
>
</td>
<td width=
>
</td>
</tr>
<tr>
<td >
</td>
<td>
</td>
<td class=
pageHeader
>
<asp:Literal ID=
ltlHeader
runat=
server
></asp:Literal>
</td>
<td>
</td>
</tr>
<tr>
<td >
</td>
<td >
</td>
<td class=
dottedLine
>
</td>
<td >
</td>
</tr>
<tr>
<td valign=
top
>
</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=
>
</td>
</tr>
<tr>
<td >
</td>
<td>
</td>
<td class=
footer
>
<table width=
%
>
<tr>
<td >
<td align=
right
>
<a _blank
>
</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
//W
C//DTD XHTML
//EN
>
<html xmlns=
>
<head runat=
server
>
<title>The
NET Pet Shop</title>
</head>
<body>
<form id=
form
runat=
server
>
<asp:Panel ID=
panFocus
runat=
server
DefaultButton=
btnSearch
>
<table align=
center
border=
cellpadding=
cellspacing=
width=
>
<tr valign=
top
>
<td rowspan=
>
<img src=
Images/Comm_Images/Logo_home
jpg
alt=
home
/></td>
<td class=
homeBgSearch
height=
width=
>
<asp:TextBox ID=
txtSearch
runat=
server
CssClass=
homeSearchBox
Width=
px
></asp:TextBox></td>
<td class=
homeBgSearch
width=
>
<asp:ImageButton ID=
btnSearch
runat=
server
AlternateText=
Search
CausesValidation=
false
CssClass=
paddingSearchicon
ImageUrl=
Images/Comm_Images/button
search
gif
/></td>
<td class=
homeBgSearch
width=
>
<asp:LoginStatus ID=
lgnStatus
runat=
server
CssClass=
homeLink
LoginText=
登 錄
LogoutAction=
Redirect
LogoutPageUrl=
~/Default
aspx
LogoutText=
退 出
/>
</td>
<td width=
>
</td>
</tr>
<tr>
<td colspan=
>
<img src=
Images/Comm_Images/KFC
JPG
/></td>
<td width=
>
</td>
</tr>
</table>
</asp:Panel>
<table align=
center
border=
cellpadding=
cellspacing=
width=
>
<tr>
<td colspan=
>
<img src=
Images/Comm_Images/spacer
gif
height=
/></td>
</tr>
<tr>
<td >
</td>
<td width=
>
</td>
<td width=
>
</td>
<td width=
>
</td>
</tr>
<tr>
<td >
</td>
<td>
</td>
<td class=
pageHeader
>
<asp:Literal ID=
ltlHeader
runat=
server
></asp:Literal>
</td>
<td>
</td>
</tr>
<tr>
<td >
</td>
<td >
</td>
<td class=
dottedLine
>
</td>
<td >
</td>
</tr>
<tr>
<td valign=
top
>
</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=
>
</td>
</tr>
<tr>
<td >
</td>
<td>
</td>
<td class=
footer
>
<table width=
%
>
<tr>
<td >
<td align=
right
>
<a _blank
>
</tr>
</table>
</td>
<td>
</td>
</tr>
</table>
</form>
</body>
</html>
代碼頁代碼
[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 =
肯德基訂餐系統
西園工作室 :: {
}
;
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 =
肯德基訂餐系統
西園工作室 :: {
}
;
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應用母版
並在後台添加代碼
設置窗體標題
應用母版代碼
[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=
ItemsControl
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=
ItemsControl
runat=
server
/>
</asp:Content>
設置窗體標題代碼
[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
])
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/13079.html