博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网上购物系统(Task008)——用户界面层公共函数集WebUtility
阅读量:6910 次
发布时间:2019-06-27

本文共 5328 字,大约阅读时间需要 17 分钟。

源代码: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的生命期。

 

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" %>    The .NET Pet Shop    
home
     
     
 
 
 
     

 

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" %>

 

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"]);        }    }}

版权所有©2012,西园电脑工作室.欢迎转载,转载请注明出处.更多文章请参阅博客

转载于:https://www.cnblogs.com/WestGarden/archive/2012/05/10/3138430.html

你可能感兴趣的文章
详解DNS的常用记录(上):DNS系列之二
查看>>
重定向与管道
查看>>
redhat6.1 X86-64 使用centos的源做yum
查看>>
闭包那一点事
查看>>
开源文化
查看>>
shell 中eval运用
查看>>
Scala之旅-class和object详解
查看>>
DNS服务的搭建 windows server 2008
查看>>
mysql报错err1055
查看>>
OpenStack 学习笔记(六):OpenStack neutron服务搭建
查看>>
shell中的算数比较
查看>>
quota(linux下的磁盤配額)
查看>>
【MySQL数据库开发之四】MySQL 处理模式/常用查询/模式匹配等
查看>>
Linux NFS服务器配置
查看>>
C++实现计数排序
查看>>
PPT组件Aspose.Slides V17.8发布 | 支持PP2010 PPTX与嵌入式视频
查看>>
Postfix全功能 (1)
查看>>
DOS系统功能调用表(INT 21H)
查看>>
作为JavaScript开发人员,这些必备的VS Code插件你都用过吗
查看>>
未来云世界畅想
查看>>