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

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

IBasePage.cs文件如下:

复制代码 代码如下:
///
/// 用于页面或用户控件
///

public interface IBasePage
{
        ///
        /// 数据缓存类
        ///

        DbCache CacheHelp { get; }

        ///


        /// 页面视图,用于页面级别访问,模仿MVC
        ///

        ViewDataDict ViewData { get; }

        ///


        /// 获取DataTable.Rows,此方法不会出错异常
        ///

        /// 数据名称
        ///
        DataRowCollection ViewDataRows(string pName);

        ///


        /// 1:true否则false
        ///

        ///
        ///
        bool IsViewData(string pName);

        ///


        /// URL参数类
        ///

        UrlQuery UQ { get; }

        ///


        /// 页面搜索关键词,搜索关键词是最基本功能,故在这里放一个字段
        ///

        string searchStr { get; set; }

        ///


        /// 权限使用次数
        ///

        int UserFunCount { get; set; }

        ///


        /// 数据库链接次数
        ///

        int DbCount { get; set; }

        ///


        /// 用与判断增加style,js等是否已重复添加
        ///

        ///
        ///
        TempListHandler TempList { get; }
}

BasePage.cs文件如下:

复制代码 代码如下:
///
/// 整站页面基础类
///

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
        ///


        /// 将数据保存在页面控件里
        ///

        public string ViewDataPage
        {
            get
            {
                return Req.Form("__viewdatapage");
            }
            set
            {
                this.ClientScript.RegisterHiddenField("__viewdatapage", value);

                //this.ClientScript.RegisterClientScriptBlock(this.GetType(), "__urlreferrer", "");
            }
        }
        #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重写获取参数
        ///


        /// 跟根URL重写获取参数
        ///

        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(@"

Number:" + __i + ", Time:" + ts.TotalMilliseconds + "ms, DB=" + this.dbCount + ",fun=" + this.userFunCount + "
");
        }
        #endregion

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

        #region 将搜索到的结果为醒目显示
        ///


        /// 将搜索到的结果为醒目显示
        ///

        ///
        ///
        public string ReplaceSearchText(object str)
        {
            if (searchStr == null || searchStr.Length == 0)
                return str.ToString();

            //return str.ToString().Replace(searchStr, "" + searchStr + "");

            return Regex.Replace(str.ToString(), Regex.Escape(searchStr), "" + searchStr + "", RegexOptions.IgnoreCase);
        }
        #endregion

        #region IBasePage 成员
        ///


        /// 数据缓存类
        ///

        public DbCache CacheHelp
        {
            get { return this.cacheHelp; }
        }

        ///


        /// 页面视图,用于页面级别访问,模仿MVC
        ///

        public ViewDataDict ViewData
        {
            get { return this.viewData; }
        }

        ///


        /// 页面视图(Model),用于页面级别访问,模仿MVC
        ///

        public ViewDataDict ViewDataM
        {
            get { return this.viewDataM; }
        }

        ///


        /// 获取DataTable.Rows,此方法不会出错异常
        ///

        ///
        ///
        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; }
        }

        /////


        ///// 获取系统配置
        /////

        /////
        /////
        //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;
        /////


        ///// 数据库连接次数
        /////

        //public int DbConnectionCount
        //{
        //    get
        //    {
        //        return this.dbConnectionCount;
        //    }
        //    set
        //    {
        //        dbConnectionCount = value;
        //    }
        //}

        #endregion

        #region 获取或设置提交后返回的路径,若未指定,则是当前url
        ///


        /// 获取或设置提交后返回的路径,若未指定,则是当前url
        ///

        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#程序设计有所帮助。

你可能感兴趣的:(C# Page用于各页面继承功能实例)