程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# Page用於各頁面繼續功效實例

C# Page用於各頁面繼續功效實例

編輯:C#入門知識

C# Page用於各頁面繼續功效實例。本站提示廣大學習愛好者:(C# Page用於各頁面繼續功效實例)文章只能為提供參考,不一定能成為您想要的結果。以下是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 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 WordStrSearchText(object str)
        {
            if (searchStr == null || searchStr.Length == 0)
                return str.ToString();

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

            return Regex.WordStr(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#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved