ASP.ASP.NET應用模板採用

來源:互聯網
上載者:User
初學ASP,程式是能勉強寫出來了,但若每進行一次網站頁面的改版,所有的來源程式都將進行一次移植手術。為此所耗費的人力精力不計其數,甚至一不小心得不償失、前功盡棄。

所以,夢想著那麼大段的程式碼變成幾個簡單的字元代替,這樣只要設計好頁面把該功能插入就OK了。其實這也簡單,只需將實現該功能的程式碼做成子程式,然後首頁調用就可以了。

很多時候,在部落格中國,你會選擇到很多的模板,甚至有可能自己來設計;或者採用豬飛飛BLOG的各大站長都將其網站改得不近相同……這些,我們都歸功於ASP採用模板的功能。

那下面偶就借花獻佛,將模板拿來分析,以饋各位朋友。

首先,模板需要線上修改,則應採用資料庫儲存模板代碼

所謂的模板,就是設計完工的標準的HTML代碼,其中需要由程式實現的功能部分將採用特殊字元串代替。然,這些特殊字元串需要在顯示的時候被編譯為對應的功能。

1,設計資料庫testmb.mdb
建立表moban:欄位m_id(自動編號,主關鍵字);欄位m_html(備忘類型)

2,假設第一模板內容代碼

將下列代碼拷貝到m_html欄位中

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>testmb</title>
</head>
<body leftmargin="0" topmargin="0">
<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="2">
  <tr align="right" bgcolor="#CCCCCC"> 
    <td height="20" colspan="2">$cntop$</td>
  </tr>
  <tr valign="top"> 
    <td width="25%" bgcolor="#e5e5e5">$cnleft$</td>
    <td width="74%" bgcolor="#f3f3f3">$cnright$</td>
  </tr>
</table>
</body>
</html>

注意$cntop$、$cnleft$、$cnright$,它們將要實現某些具體的程式功能

3,建立資料庫連接檔案conn.asp

<%
set conn= Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("testmb.mdb")
conn.Open connstr
%>

4,建立特殊字元串轉換所需要的庫檔案lib.asp

該檔案的主要作用是將實現某些功能的ASP程式做成字程式,以方便調用。

<%
dim topcode
sub cntop()
    topcode="現在時間是:"
    topcode=topcode&now()
end sub

dim leftcode,i
sub cnleft()
    for i = 1 to 5
    leftcode=leftcode&"<p>cnbruce.com"
    next
end sub

dim rightcode
sub cnright()
    for i = 1 to 9
    rightcode=rightcode&"<hr color="&i&i&i&i&i&i&">"
    next
end sub 
%>

5,最後,調用資料庫中的模板代碼,將特殊字元串轉換。

<!--#include file="conn.asp" -->
<!--#include file="lib.asp" -->
<%
sql="select * from moban where m_id=1"
set rs=Server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
mb_code=rs("m_html")
rs.close
set rs=nothing

cntop()
mb_code=replace(mb_code,"$cntop$",topcode)
cnleft()
mb_code=replace(mb_code,"$cnleft$",leftcode)
cnright()
mb_code=replace(mb_code,"$cnright$",rightcode)

response.write mb_code
%>

該頁主要作用是將模板代碼進行顯示,並將其中的特殊代碼轉變為相對應子程式功能。

至此,ASP的模板功能基本完成,剩下的就是:建立具備編輯模板功能的程式頁面,將庫檔案改變為自己所需要程式功能……

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=783511

ASP.NET中應用模板採用

1。使用者介面

 using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
namespace localhost.模版後台管理
{
 /// <summary>
 /// UseInterface 的摘要說明。
 /// </summary>
 public class UseInterface : System.Web.UI.Page
 {
  protected string show_string;
  protected StringBuilder show_string2;
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此處放置使用者代碼以初始化頁面
   SqlConnection Conn;
   Conn=new SqlConnection("server=localhost;database=Skin;uid=sa;pwd=''");
   Conn.Open();   
   SqlDataAdapter da=new  SqlDataAdapter("select Skin_html from SkinBin where IsDefault=1",Conn);   
   DataSet ds=new DataSet();
   da.Fill(ds,"skin");
//   show_string2.Append(ds.Tables["skin"].Rows[0][0].ToString());
//   show_string2.Replace("$show_blogname$","無影殺手");//替換特殊標記
   show_string=ds.Tables["skin"].Rows[0][0].ToString();
   show_string=show_string.Replace("$show_blogname$","無影殺手");
   Response.Write(show_string);   
  }

  #region Web Form設計器產生的程式碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
 }
}

2。管理後台

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace localhost.模版後台管理
{
 /// <summary>
 /// SkinManage 的摘要說明。
 /// </summary>
 public class SkinManage : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DataGrid DataGrid2;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此處放置使用者代碼以初始化頁面
   // 在此處放置使用者代碼以初始化頁面
   SqlConnection Conn;
   Conn=new SqlConnection("server=localhost;database=Skin;uid=sa;pwd=''");
   Conn.Open();   
   SqlDataAdapter da=new SqlDataAdapter("select Id,Skin_name from SkinBin",Conn);
   DataSet ds=new DataSet();
   da.Fill(ds,"skin");
   da.Fill(ds,"skinall");
//   show_string=ds.Tables["skin"].Rows[0][0].ToString();   
   DataGrid2.DataSource=ds.Tables["skinall"].DefaultView;
   DataGrid2.DataBind();
  }

  #region Web Form設計器產生的程式碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.DataGrid2.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid2_DeleteCommand);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void DataGrid2_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   try
   {
    SqlConnection Conn;
    Conn=new SqlConnection("server=localhost;database=Skin;uid=sa;pwd=''");
    Conn.Open();
    string Id="";
    Id=e.Item.Cells[2].Text;
    SqlCommand Comm=new SqlCommand("update SkinBin set IsDefault=1 where Id="+Id,Conn);
    Comm.ExecuteNonQuery();
   }
   catch
   {

   }
   Response.Write("<script>alert('模版已更改,請查看使用者介面');</script>");

  }
 

  
 }
}

3。資料庫

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SkinBin]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[SkinBin]
GO

CREATE TABLE [dbo].[SkinBin] (
 [Id] [int] IDENTITY (1, 1) NOT NULL ,
 [Skin_name] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
 [Skin_html] [ntext] COLLATE Chinese_PRC_CI_AS NULL ,
 [IsDefault] [bit] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=668003

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.