博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
1211Bug with integer literals in PLSQL
查看>>
Linux 权限管理之目录权限限制
查看>>
再谈矩阵分解在推荐系统中的应用
查看>>
ABAP 面试问题及答案(一):数据库更新及更改 SAP Standard (转)
查看>>
Top 10 JavaScript编辑器,你在用哪个?
查看>>
数据访问层的优化思路
查看>>
饭后最该知道N件事
查看>>
MaxCompute Studio 2.8.1 新版本发布啦!
查看>>
《区块链原理、设计与应用》一2.5 认识上的误区
查看>>
当所有编程语言都在靠齐的时候
查看>>
苹果拥抱IBM背后:大数据推动手机行业洗牌
查看>>
无线广播可以毁灭物联网安全:信号干扰器及犯罪
查看>>
《并行计算的编程模型》一3.7.1 选择集合参与者
查看>>
百分点:利用大数据做智慧商业
查看>>
让你的软件永生的7个规则
查看>>
浅析自动化设备安装运维的发展方向
查看>>
2015年Facebook广告变现规模达10亿美金
查看>>
澳大利亚推出网安行业竞争力计划
查看>>
人类与机器人,如何能像朋友一样愉快聊天?
查看>>
2013云计算预测:攻击者馋涎云中数据
查看>>