web前端

位置:IT落伍者 >> web前端 >> 浏览文章

网购系统——用户界面层公共函数集WebUtility


发布日期:2022年12月18日
 
网购系统——用户界面层公共函数集WebUtility
频繁的数据库操作需要一个公共的数据库操作函数集(DBUtility中的SQLHelpercs)频繁的用户界面操作也需要一个公共函数集WebUtilitycs因为频繁这个类及类中的函数也做成了静态的

App_Code中添加类WebUtilitycs并在类中添加函数GetCategoryName()

[csharp] using System;

using SystemConfiguration;

using SystemWeb;

using SystemWebCaching;

using WestGardenDAL;

namespace WestGardenWeb

{

public static class WebUtility

{

private const string CATEGORY_NAME_KEY = category_name_{};

private static readonly bool enableCaching = boolParse(ConfigurationManagerAppSettings[EnableCaching])

public static string GetCategoryName(string categoryId)

{

Category category = new Category()

if (!enableCaching)

return categoryGetCategory(categoryId)Name;

string cacheKey = stringFormat(CATEGORY_NAME_KEY categoryId)

string data = (string)HttpRuntimeCache[cacheKey];

if (data == null)

{

int cacheDuration = intParse(ConfigurationManagerAppSettings[CategoryCacheDuration])

data = categoryGetCategory(categoryId)Name;

HttpRuntimeCacheAdd(cacheKey data null DateTimeNowAddHours(cacheDuration) CacheNoSlidingExpiration CacheItemPriorityHigh null)

}

return data;

}

}

}

using System;

using SystemConfiguration;

using SystemWeb;

using SystemWebCaching;

using WestGardenDAL;

namespace WestGardenWeb

{

public static class WebUtility

{

private const string CATEGORY_NAME_KEY = category_name_{};

private static readonly bool enableCaching = boolParse(ConfigurationManagerAppSettings[EnableCaching])

public static string GetCategoryName(string categoryId)

{

Category category = new Category()

if (!enableCaching)

return categoryGetCategory(categoryId)Name;

string cacheKey = stringFormat(CATEGORY_NAME_KEY categoryId)

string data = (string)HttpRuntimeCache[cacheKey];

if (data == null)

{

int cacheDuration = intParse(ConfigurationManagerAppSettings[CategoryCacheDuration])

data = categoryGetCategory(categoryId)Name;

HttpRuntimeCacheAdd(cacheKey data null DateTimeNowAddHours(cacheDuration) CacheNoSlidingExpiration CacheItemPriorityHigh 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()函数为此需要在Categorycs中添加函数GetCategory()

[csharp] public CategoryInfo GetCategory(string categoryId)

{

CategoryInfo category = null;

SqlParameter parm = new SqlParameter(Parm_CATEGORY_ID SqlDbTypeVarChar

parmValue = categoryId;

using (SqlDataReader rdr = SqlHelperExecuteReader(SqlHelperConnectionStringLocalTransaction CommandTypeText SQL_SELECT_CATEGORIES parm))

{

if (rdrRead())

category = new CategoryInfo(rdrGetString( rdrGetString( rdrGetString())

else

category = new CategoryInfo()

}

return category;

}

public CategoryInfo GetCategory(string categoryId)

{

CategoryInfo category = null;

SqlParameter parm = new SqlParameter(Parm_CATEGORY_ID SqlDbTypeVarChar

parmValue = categoryId;

using (SqlDataReader rdr = SqlHelperExecuteReader(SqlHelperConnectionStringLocalTransaction CommandTypeText SQL_SELECT_CATEGORIES parm))

{

if (rdrRead())

category = new CategoryInfo(rdrGetString( rdrGetString( rdrGetString())

else

category = new CategoryInfo()

}

return category;

}

Web添加母版MasterPagemaster

窗体页代码

[html] <%@ Master Language=C# AutoEventWireup=true CodeFile=MasterPagemastercs Inherits=WestGardenWebMasterPage %>

<%@ Register Src=Controls/NavigationControlascx TagName=NavigationControl TagPrefix=WestGardenControl %>

<!DOCTYPE html PUBLIC //WC//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_homejpg 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/buttonsearchgif /></td>

<td class=homeBgSearch width=>

<asp:LoginStatus ID=lgnStatus runat=server CssClass=homeLink LoginText=登 录

LogoutAction=Redirect LogoutPageUrl=~/Defaultaspx LogoutText=退 出 />

</td>

<td width=>

</td>

</tr>

<tr>

<td colspan=>

<img src=Images/Comm_Images/KFCJPG /></td>

<td width=>

</td>

</tr>

</table>

</asp:Panel>

<table align=center border= cellpadding= cellspacing= width=>

<tr>

<td colspan=>

<img src=Images/Comm_Images/spacergif 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=MasterPagemastercs Inherits=WestGardenWebMasterPage %>

<%@ Register Src=Controls/NavigationControlascx TagName=NavigationControl TagPrefix=WestGardenControl %>

<!DOCTYPE html PUBLIC //WC//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_homejpg 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/buttonsearchgif /></td>

<td class=homeBgSearch width=>

<asp:LoginStatus ID=lgnStatus runat=server CssClass=homeLink LoginText=登 录

LogoutAction=Redirect LogoutPageUrl=~/Defaultaspx LogoutText=退 出 />

</td>

<td width=>

</td>

</tr>

<tr>

<td colspan=>

<img src=Images/Comm_Images/KFCJPG /></td>

<td width=>

</td>

</tr>

</table>

</asp:Panel>

<table align=center border= cellpadding= cellspacing= width=>

<tr>

<td colspan=>

<img src=Images/Comm_Images/spacergif 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 SystemWeb;

using SystemWebUIWebControls;

namespace WestGardenWeb

{

public partial class MasterPage : SystemWebUIMasterPage

{

private const string HEADER_PREFIX = 肯德基订餐系统西园工作室 :: {};

protected void Page_PreRender(object sender EventArgs e)

{

ltlHeaderText = PageHeaderTitle;

PageHeaderTitle = stringFormat(HEADER_PREFIX PageHeaderTitle)

}

}

}

using System;

using SystemWeb;

using SystemWebUIWebControls;

namespace WestGardenWeb

{

public partial class MasterPage : SystemWebUIMasterPage

{

private const string HEADER_PREFIX = 肯德基订餐系统西园工作室 :: {};

protected void Page_PreRender(object sender EventArgs e)

{

ltlHeaderText = PageHeaderTitle;

PageHeaderTitle = stringFormat(HEADER_PREFIX PageHeaderTitle)

}

}

}

为已建窗体Itemsaspx应用母版并在后台添加代码设置窗体标题

应用母版代码

[html] <%@ Page Language=C# MasterPageFile=~/MasterPagemaster AutoEventWireup=true CodeFile=Itemsaspxcs Inherits=WestGardenWebItems %>

<%@ Register Src=Controls/ItemssControlascx 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=~/MasterPagemaster AutoEventWireup=true CodeFile=Itemsaspxcs Inherits=WestGardenWebItems %>

<%@ Register Src=Controls/ItemssControlascx TagName=ItemsControl TagPrefix=WestGardenControl %>

<asp:Content ID=cntPage ContentPlaceHolderID=cphPage runat=Server EnableViewState=false>

<WestGardenControl:ItemsControl ID=ItemsControl runat=server />

</asp:Content>

设置窗体标题代码

[csharp] using WestGardenDAL;

namespace WestGardenWeb

{

public partial class Items : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

PageTitle = WebUtilityGetCategoryName(RequestQueryString[categoryId])

}

}

}

               

上一篇:开发基于Web的CSS设计器

下一篇:自定义创建web验证控件