在网上看了许多能生成静态页的新闻系统,但基于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
}
}
新闻热点
疑难解答
图片精选