C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# Page用于各页面继承功能

C# Page用于各页面继承功能实例

投稿:shichen2014

这篇文章主要介绍了C# Page用于各页面继承功能实例,包含了常见的页面视图、数据缓存、数据库操作等技巧,需要的朋友可以参考下

本文实例讲述了C# Page用于各页面继承功能的实现方法。分享给大家供大家参考。具体方法如下:

IBasePage.cs文件如下:

复制代码 代码如下:
/// <summary>
/// 用于页面或用户控件
/// </summary>
public interface IBasePage
{
        /// <summary>
        /// 数据缓存类
        /// </summary>
        DbCache CacheHelp { get; }

        /// <summary>
        /// 页面视图,用于页面级别访问,模仿MVC
        /// </summary>
        ViewDataDict ViewData { get; }

        /// <summary>
        /// 获取DataTable.Rows,此方法不会出错异常
        /// </summary>
        /// <param name="pName">数据名称</param>
        /// <returns></returns>
        DataRowCollection ViewDataRows(string pName);

        /// <summary>
        /// 1:true否则false
        /// </summary>
        /// <param name="pName"></param>
        /// <returns></returns>
        bool IsViewData(string pName);

        /// <summary>
        /// URL参数类
        /// </summary>
        UrlQuery UQ { get; }

        /// <summary>
        /// 页面搜索关键词,搜索关键词是最基本功能,故在这里放一个字段
        /// </summary>
        string searchStr { get; set; }

        /// <summary>
        /// 权限使用次数
        /// </summary>
        int UserFunCount { get; set; }

        /// <summary>
        /// 数据库链接次数
        /// </summary>
        int DbCount { get; set; }

        /// <summary>
        /// 用与判断增加style,js等是否已重复添加
        /// </summary>
        /// <param name="pName"></param>
        /// <returns></returns>
        TempListHandler TempList { get; }
}

BasePage.cs文件如下:

复制代码 代码如下:
/// <summary>
/// 整站页面基础类
/// </summary>
public class BasePage : System.Web.UI.Page, IBasePage
{
        private static readonly DataRowCollection EmptyRows = new DataTable().Rows;

        private DbCache cacheHelp;
        private ViewDataDict viewData;
        private ViewDataDict viewDataM;
        private int userFunCount = 0;//权限使用次数
        private int dbCount = 0;//数据库连接次数

        private TempListHandler tempList;

        protected bool IsOpenXingNengTongji = true;

        #region 搜索关键词
        //private string _searchStr;
        public string searchStr
        {
            get
            {
                //if (this._searchStr == null)
                //{
                //    return string.Empty;
                //}

                return ViewData["keyword"].ToString();
            }
            set
            {
                //this._searchStr = value;
                ViewData["keyword"] = value;
            }
        }
        #endregion

        #region 将数据保存在页面控件里 ViewDataPage
        /// <summary>
        /// 将数据保存在页面控件里
        /// </summary>
        public string ViewDataPage
        {
            get
            {
                return Req.Form("__viewdatapage");
            }
            set
            {
                this.ClientScript.RegisterHiddenField("__viewdatapage", value);

                //this.ClientScript.RegisterClientScriptBlock(this.GetType(), "__urlreferrer", "<input id=\"__urlreferrer\" name=\"__urlreferrer\" type=\"hidden\" value=\"" + value + "\" />");
            }
        }
        #endregion

        public BasePage()
        {
            cacheHelp = new DbCache();
            viewData = new ViewDataDict();
            viewDataM = new ViewDataDict("model_");

            tempList = new TempListHandler();
        }

        private DateTime __time1;
        private DateTime __time2;
        private static int __i = 0;

        #region 跟根URL重写获取参数
        /// <summary>
        /// 跟根URL重写获取参数
        /// </summary>
        private UrlQuery _UQ;

        protected override void OnInit(EventArgs e)
        {
            _UQ = new UrlQuery(true);

            __time1 = DateTime.Now;

            base.OnInit(e);
        }

        protected override void OnPreInit(EventArgs e)
        {
            //"OnPreInit".WriteLine();
            base.OnPreInit(e);
        }
        #endregion

        #region 基本信息统计
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            //"Render".WriteLine();
            base.Render(writer);
            __i++;
            __time2 = DateTime.Now;
            TimeSpan ts = __time2 - __time1;

            if (IsOpenXingNengTongji)
                Response.Write(@"<div style=""text-align:center;color:#ccc"">Number:" + __i + ", Time:" + ts.TotalMilliseconds + "ms, DB=" + this.dbCount + ",fun=" + this.userFunCount + "</div>");
        }
        #endregion

        protected override void OnUnload(EventArgs e)
        {
            //"OnUnload".WriteLine();
            base.OnUnload(e);
        }

        #region 将搜索到的结果为醒目显示
        /// <summary>
        /// 将搜索到的结果为醒目显示
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string ReplaceSearchText(object str)
        {
            if (searchStr == null || searchStr.Length == 0)
                return str.ToString();

            //return str.ToString().Replace(searchStr, "<strong style=\"color:#F00\">" + searchStr + "</strong>");

            return Regex.Replace(str.ToString(), Regex.Escape(searchStr), "<strong style=\"color:#F00\">" + searchStr + "</strong>", RegexOptions.IgnoreCase);
        }
        #endregion

        #region IBasePage 成员
        /// <summary>
        /// 数据缓存类
        /// </summary>
        public DbCache CacheHelp
        {
            get { return this.cacheHelp; }
        }

        /// <summary>
        /// 页面视图,用于页面级别访问,模仿MVC
        /// </summary>
        public ViewDataDict ViewData
        {
            get { return this.viewData; }
        }

        /// <summary>
        /// 页面视图(Model),用于页面级别访问,模仿MVC
        /// </summary>
        public ViewDataDict ViewDataM
        {
            get { return this.viewDataM; }
        }

        /// <summary>
        /// 获取DataTable.Rows,此方法不会出错异常
        /// </summary>
        /// <param name="pName"></param>
        /// <returns></returns>
        public DataRowCollection ViewDataRows(string pName)
        {
            DataTable dt = ViewData[pName] as DataTable;

            if (dt == null)
            {
                return EmptyRows;
            }
            return dt.Rows;
        }

        public bool IsViewData(string pName)
        {
            return viewData[pName].ToString() == "1";
        }

        public UrlQuery UQ
        {
            get { return this._UQ; }
        }

        ///// <summary>
        ///// 获取系统配置
        ///// </summary>
        ///// <param name="key"></param>
        ///// <returns></returns>
        //public string SysConfig(string key)
        //{
        //    return WConfigBLL.GetValue(key).ToString();
        //}

        public TempListHandler TempList
        {
            get { return this.tempList; }
        }

        public int UserFunCount
        {
            get { return this.userFunCount; }
            set { this.userFunCount = value; }
        }

        public int DbCount
        {
            get { return this.dbCount; }
            set { this.dbCount = value; }
        }
        #endregion

        #region ITrace 成员
        //private int dbConnectionCount;
        ///// <summary>
        ///// 数据库连接次数
        ///// </summary>
        //public int DbConnectionCount
        //{
        //    get
        //    {
        //        return this.dbConnectionCount;
        //    }
        //    set
        //    {
        //        dbConnectionCount = value;
        //    }
        //}

        #endregion

        #region 获取或设置提交后返回的路径,若未指定,则是当前url
        /// <summary>
        /// 获取或设置提交后返回的路径,若未指定,则是当前url
        /// </summary>
        public string UrlReferrer
        {
            get
            {
                string s = Req.Form("___urlreferrer");
                if (s.Length == 0)
                {
                    s = ViewData["___urlreferrer"].ToString();
                }

                if (s.Length == 0)
                {
                    s = Request.Url.ToString();
                }

                return s;
            }
            set
            {
                ViewData["___urlreferrer"] = value;

                this.ClientScript.RegisterHiddenField("___urlreferrer", value);\" type=\"hidden\" value=\"" + value + "\" />");
            }
        }
        #endregion
}

希望本文所述对大家的C#程序设计有所帮助。

您可能感兴趣的文章:
阅读全文