博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

定义一个 BagelPage

Posted on 2011-03-05 16:59  CodeNote  阅读(249)  评论(0编辑  收藏  举报

感谢小山的Blog

原文地址

注意定义的属性部分。

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

    }
}

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()
        
{
            
        }

        
        
#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


        
#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


    }

}