痞子龍㊣

C#零起点

 

通用基类

using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Text.RegularExpressions;
namespace Document.Base
{
    /**//// <summary>
    /// Summary description for BasePage.
    /// </summary>
    public class BasePage : System.Web.UI.Page
    {
        public BasePage()
        {
           
        }
       
        Render#region Render
        protected override void Render(HtmlTextWriter writer)
        {           
            TextWriter tempWriter = new StringWriter();
            base.Render(new HtmlTextWriter(tempWriter));

            //1.定义页面的样式表,所有从这个基类继承而来的页面都不需要添加样式表的链接。
            string css =  AppName + "/CSS/styles.css";//自定义css样式路径
            string newCSS = "<link type=\"text/css\" rel=\"stylesheet\" href=\"" + css + "\" />";
           
            //2.定义页面的应用js,所有从这个基类继承而来的页面都不需要添加js的链接。
            string js =  AppName + "/js/Calendar.js";//自定义js路径
            string newJS = "<script src=\""+js+"\"></script>";//如果有多个js文件时,可以继续加入
            string newstr=newCSS+newJS+"</HEAD>";
            writer.Write(Regex.Replace(tempWriter.ToString(),"</HEAD>",newstr,RegexOptions.IgnoreCase));
            //原理就是把</head>标签替换为想要加的内容,然后再加上</head>标签,相当于在</head>标签前插入想要加的内容。
        }
        #endregion

        property#region property
        public string AppName
        {
            get
            {
                string appPath=Request.ApplicationPath;
                if(appPath.Trim()=="/")
                    return "";
                return appPath;
            }
        }
        //返回图片路径
        protected String ImageRoot
        {
            get
            {
                return HttpContext.Current.Request.ApplicationPath + "/Image/";
            }
        }
        //系统路径
        protected string RootPath
        {
            get
            {
                return Request.ApplicationPath;
            }
        }
        #endregion

    }
}

posted on 2006-12-28 23:17  hnsjack(痞子龍)  阅读(90)  评论(0编辑  收藏  举报

导航