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

简介: //模版页面.html 链接1链接2链接3 //Template类 using System;using System.

//模版页面.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;
            }
            else
                return temstr;
        }
        #endregion

        #region 绑定模版到页面(直接输入模版)
        /// <summary>
        /// 绑定模版到页面(直接输入模版)
        /// </summary>
        /// <param name="SourcePage"></param>
        /// <param name="PageName"></param>
        public static void BindTextToPage(Page SourcePage, string TemplateContent)
        {
            Control MyTemplateControl = new Control();
            try
            {
                MyTemplateControl = SourcePage.ParseControl(TemplateContent);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            SourcePage.Controls.Add(MyTemplateControl);
        }
        #endregion
    }
}

//测试页面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testDemo.aspx.cs" Inherits="testweb.testDemo" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <br/><a href="#" id="pagelink1" runat="server" >页面链接</a>
    </div>
    </form>
</body>

//测试页面后台

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Html=System.Web.UI.HtmlControls;
namespace testweb
{
    //Page_Init在Page_Load前执行
    public partial class testDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Html.HtmlAnchor link2 = (Html.HtmlAnchor)Page.FindControl("link2");
            Response.Write(link2.InnerHtml);
            link2.Visible = false;
        }
        protected void Page_Init(object sender, EventArgs e)
        {
            Template.BindTemplateToPage(this, "/模版页面.html");
        }
    }
}

结果:




这个是直接在本页面输出,还能用runat="server"的控件 ,

附:生成静态页方法

http://blog.sina.com.cn/s/blog_6439f26f0100zhn7.html

可结合使用。




相关文章
|
JavaScript 前端开发
vue 部署项目,访问页面空白,找不到js或css文件 (net::ERR_ABORTED 404 (Not Found))
vue 部署项目,访问页面空白,找不到js或css文件 (net::ERR_ABORTED 404 (Not Found))
1746 0
vue 部署项目,访问页面空白,找不到js或css文件 (net::ERR_ABORTED 404 (Not Found))
|
JavaScript
Vue 打包后打开为空白页面 并且控制台报错‘Failed to load resource: net::ERR_FILE_NOT_FOUND’
Vue 打包后打开为空白页面 并且控制台报错‘Failed to load resource: net::ERR_FILE_NOT_FOUND’
Vue 打包后打开为空白页面 并且控制台报错‘Failed to load resource: net::ERR_FILE_NOT_FOUND’
|
9月前
|
开发框架 人工智能 前端开发
Visual Studio Code安装C#开发工具包并编写ASP.NET Core Web应用
Visual Studio Code安装C#开发工具包并编写ASP.NET Core Web应用
191 0
|
9月前
|
开发框架 .NET C#
Visual Studio Code调试和发布ASP.NET Core Web应用
Visual Studio Code调试和发布ASP.NET Core Web应用
111 0
|
9月前
|
开发框架 数据可视化 前端开发
ASP.NET Core MVC+Quartz实现定时任务可视化管理页面
ASP.NET Core MVC+Quartz实现定时任务可视化管理页面
334 0
|
10月前
|
开发框架 JavaScript .NET
Asp.net C#页面传参的几种方式
Asp.net C#页面传参的几种方式
108 0
|
11月前
|
SQL 开发框架 安全
保护ASP.NET 应用免受 CSRF 攻击
保护ASP.NET 应用免受 CSRF 攻击
|
11月前
mvc.net分页查询案例——前台页面(Index.aspx)
mvc.net分页查询案例——前台页面(Index.aspx)
47 0
|
开发框架 JSON 前端开发
【C#】.net core2.1,自定义全局类对API接口和视图页面产生的异常统一处理
在开发一个网站项目时,异常处理和过滤功能是最基础的模块 本篇文章就来讲讲,如何自定义全局异常类来统一处理
207 0