asp.net模版页面的高级应用
!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; 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 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(); 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
可结合使用。
asp.net core重新加载应用配置 asp.net core重新加载应用配置Intro#我把配置放在了数据库或者是Redis里,配置需要修改的时候我要直接修改数据库,然后调用一个接口去重新加载应用配置,于是就尝试写一个运行时重新加载配置的接口。
在Azure中创建asp.net core 应用 0、前言 在玩转Azure之前首先大家要有Azure账号,或者可以先申请一下微软的账号,然后进行与Azure的关联(azure账号是免费的)。但是关联的步骤还是很有意思的,他需要VISA国际信用卡(我申请的是国际版的账号),楼主我本身就是大土豪(搞得好像很有钱似的), 重来不申请什么国际信用卡,都是国内银联信用卡搞的溜溜的,好吧为了搞一下azure,就申请了一张招商银行的VISA全币种国际信息卡(此处打个广告),不出3天就到了。
相关文章
- 【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)
- 【翻译】在Mac上使用VSCode创建你的第一个Asp.Net Core应用
- ASP.NET MVC项目中App_Code目录在程序应用
- ASP.NET MVC中jQuery与angularjs混合应用传参并绑定数据
- ASP.NET截取网页注释行之间的内容
- ASP.NET MVC加载用户控件后并获取其内控件值或赋值
- ASP.NET Core 1.0 Configuration 配置管理
- 13、ASP.NET MVC入门到精通——MVC请求管道
- 如何解决ASP.NET网站'__doPostBack' is undefined的脚本错误
- ASP.NET Core Library – HtmlSanitizer
- (四)Asp.net web api中的坑-【api的返回值】
- IoC在ASP.NET Web API中的应用
- 通过3个Hello World应用来了解ASP.NET 5应用是如何运行的(1)
- [ASP.NET MVC] 利用自定义的AuthenticationFilter实现Basic认证
- ASP.NET Web API中的Controller
- ASP.NET MVC的Razor引擎:IoC在View激活过程中的应用
- Asp.net的sessionState四种模式配置方案
- ASP.NET Core 发布至Linux生产环境 Ubuntu 系统