日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/7/14 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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;}elsereturn 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

可結合使用。




轉載于:https://www.cnblogs.com/pukuimin/archive/2013/03/16/2988068.html

總結

以上是生活随笔為你收集整理的asp.net模版页面的高级应用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。