在網上看了許多能生成靜態頁的新聞系統,但基於ASP.Net的系統極少,閒下時間來自己寫了一個,發出來,大家一起研究,代碼沒做什麼優化,只是實現了功能
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
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.Configuration;
namespace makeHtmlfile
{
/// <summary>
/// makeallfiles 的摘要說明。
/// </summary>
public class makeallfiles : System.Web.UI.Page
{
public string strcon;
public OleDbConnection conn;
public string strSQL;
private void Page_Load(object sender, System.EventArgs e)
{
InitialPages();// 在此處放置用戶代碼以初始化頁面
}
public void InitialPages()
{
strcon = "provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.APPSettings["MDBpath2"])+";";//連接字符竄// 在此處放置用戶代碼以初始化頁面
strSQL = "select id,class1id,class2id from news order by id desc";
MakeAreaForShow();
ReadNewsForWriteFileUserDataReader(); //同過DataReader來讀取數據,
//ReadNewsForWriteFileUserDataSet(); //將數據直接掛入DataSet中來讀取,
}
/// <summary>
/// 用來產生循環顯示頁面的區域,裝載生成Html頁的ASPX頁面的區域
/// </summary>
public void MakeAreaForShow()
{
Response.Write("<span id=showImport></span>");
Response.Write("<IE:Download ID='oDownload' STYLE='behavior:url(#default#download)'/>");
}
/// <summary>
/// 通過DATAREADER來讀取數據
/// </summary>
public void ReadNewsForWriteFileUserDataReader()
{
int num = 0 ;
string newsid = null;
string class1id = null;
string class2id = null;
OleDbDataReader dr = null;
OleDbConnection conn = new OleDbConnection(strcon);
conn.Open();
OleDbCommand mycommand = new OleDbCommand(strSQL,conn);
dr = mycommand.ExecuteReader();
while(dr.Read())
{
newsid = dr["id"].ToString();
class1id = dr["class1id"].ToString();
class2id = dr["class2id"].ToString();
WriteJScript(newsid,class1id,class2id);
num++;
}
dr.Close();
conn.Close();
Response.Write(num.ToString());
}
/// <summary>
/// 通過DATASET來讀取數據
/// </summary>
public void ReadNewsForWriteFileUserDataSet()
{
DataSet ds = new DataSet();
int num = 0 ;
string newsid = null;
string class1id = null;
string class2id = null;
OleDbConnection conn = new OleDbConnection(strcon);
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(strSQL,conn);
da.Fill(ds,"news");
conn.Close();
num = ds.Tables["news"].Rows.Count;
foreach(DataRow dr in ds.Tables["news"].Rows)
{
newsid = dr["id"].ToString();
class1id = dr["class1id"].ToString();
class2id = dr["class2id"].ToString();
WriteJScript(newsid,class1id,class2id);
}
ds = null;
Response.Write(num.ToString());
}
public void WriteJScript(string newsid,string class1id,string class2id)
{
Response.Write("<script>");
Response.Write("function onDownloadDone(downDate)");
Response.Write("{");
Response.Write("showImport.innerHtml=downDate");
Response.Write("}");
Response.Write("oDownload.startDownload('makefile2.ASPx?id=");
Response.Write(newsid);
Response.Write("&class1id=");
Response.Write(class1id);
Response.Write("&class2id=");
Response.Write(class2id);
Response.Write("',onDownloadDone)");
Response.Write("</script>");
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.Net Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}