日期:2014-05-16  浏览次数:21078 次

asp.net模版页面的高级应用

//模版页面.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<a href="#">链接1</a><br/>
<a href="#" id="link2" runat="server">链接2</a><br/>
<a href="#">链接3</a>
</body>
</html>

//Template类

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;

namespace testweb
{
    public class Template
    {
        #region 绑定模版到页面
        /// <summary>
        /// 绑定模版到页面  
        /// </summary>
        /// <param name="SourcePage"></param>
        /// <param name="PageName"></param>
        public static void BindTemplateToPage(Page SourcePage, string PageName)
        {

            string templatepath = PageName;
            if (templatepath != null && templatepath != "")
            {
                string TemplateContent = GetTemplate(templatepath);

                BindTextToPage(SourcePage, TemplateContent);
            }
        }
        #endregion

        #region 根据模板路径读取模板内容 
        /// <summary>
        /// 根据模板路径读取模板内容
        /// </summary>
        /// <param name="TemplatePath">模板(相对站点根)路径</param>
        /// <returns>返回 string</returns>
        public static string GetTemplate(string TemplatePath)
        {
            string m_path = HttpContext.Current.Server.MapPath("~/");

            m_path = m_path + TemplatePath;
            string str;
            Encoding code = Encoding.GetEncoding(0);
            StreamReader sr = null;

            try
            {
                sr = new StreamReader(m_path, code);
                str = sr.ReadToEnd();
                sr.Close();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message.ToString());
            }

            str = RepaceRequest(str);
            return str;
        }
        #endregion

        #region 替换Url请求标签
        /// <summary>
        /// 替换Url请求标签
        /// </summary>
        /// <param name="temstr"></param>
        /// <returns></returns>
        public static string RepaceRequest(string temstr)
        {
            String Pattern = @"{@(.*?)}";
            MatchCollection Matches = Regex.Matches(temstr, Pattern, RegexOptions.IgnoreCase
           | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture);
            if (Matches.Count > 0)
            {
                for (int i = 0; i < Matches.Count; i++)
                {
                    int m_length = Matches[i].Value.Length - 3;
                    string ParterName = Matches[i].Value.Substring(2, m_length).Trim();
                    string ParterValue = HttpContext.Current.Request[ParterName];
                    if (ParterValue == null)
                    {
                        ParterValue = "";
                    }
                    else
                    {
                        try
                        {
                            Int32.Parse(ParterValue);
                        }
                        catch
                        {
                            ParterValue = "";
                        }
                    }
                    temstr = temstr.Replace(Matches[i].Value, ParterValue.ToString().Trim());
                }
                //temstr = temstr.Replace("剩余时间", "距开始时间");
                return temstr;