今天学会用静态页面动态显示,方法很简单.
首先建一页面叫aa.aspx.
aa.aspx用来显示新闻.在 <form id="form1" runat="server">
<script language="javascript" src="bb.aspx" type="text/javascript"></script>
</form>
然后建一页面叫bb.aspx.
在该页下的.
protected void Page_Load(object sender, EventArgs e)
{
string sql = "select * from article";
DataSet ss = bll.GreatDs(sql);
DataTable dt = ss.Tables[0];
Response.Write("document.write('<table cellspacing=0 rules=/"all/" border=1 id=/"ctl00_ContentPlaceHolder1_GridView1/" style=/"border-collapse:collapse;/">');");
Response.Write("document.write('<tr><th scope=/"col/">序号</th><th scope=/"col/">文章类型</th><th scope=/"col/">标题</th><th scope=/"col/">更新时间</th><th scope=/"col/">点击率</th>');");
Response.Write("document.write('</tr>');");
for (int i=0; i < dt.Rows.Count; i++)
{
Response.Write("document.write('<tr>');");
Response.Write("document.write('<td>"+dt.Rows[i]["articleid"].ToString()+"');");
Response.Write("document.write('</td>');");
Response.Write("document.write('<td>" + dt.Rows[i]["class"].ToString() + "');");
Response.Write("document.write('</td>');");
Response.Write("document.write('<td><a href=/"newsDetail.aspx?id=" + dt.Rows[i]["articleid"].ToString() + "/">" + dt.Rows[i]["title"].ToString() + "</a>');");
Response.Write("document.write('</td>');");
Response.Write("document.write('<td>" + dt.Rows[i]["dateandtime"].ToString() + "');");
Response.Write("document.write('</td>');");
Response.Write("document.write('<td>" + dt.Rows[i]["hits"].ToString() + "');");
Response.Write("document.write('</td>');");
Response.Write("document.write('</tr>');");
}
Response.Write("document.write('</table>');");
Response.End();
}
写上以上语句.就可以像用gridview一样了.
呵呵.简单又方便.
这种方法并不能提高网页打开的速度,所以我后来采用用生成静态页的方式来实现。具体如下:
private void createHtml()
{
try
{
string sql = "select distinct top 6 a.articleid,b.class,a.title,a.dateandtime,a.hits from article a,aclass b where a.classid=b.classid order by a.articleid desc";
DataSet ss = spbase.GreatDs(sql);
DataTable dt = ss.Tables[0];
if (dt == null) return;
string cription = "";
string title = "新闻";//这是动态生成的静态页面
string dhtml = "<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.0 Transitional//CN/"><html><head>" +
"<meta http-equiv=/"Content-Type/" content=/"text/html; charset=utf-8/">" +
"<title>" + title + "</title><link href=/"../Style2.CSS/" rel=/"stylesheet/" type=/"text/css/" /></head><body topmargin=0>";
dhtml += "<table valign=top border=0 cellspacing=0 cellpadding=0 align=left>" +
"<tr>";//<tr><td height=5>" + title + "</td></tr>
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["title"].ToString().Length > 25)
{
cription = dt.Rows[i]["title"].ToString().Substring(0, 25) + "....";
}
else
cription = dt.Rows[i]["title"].ToString();
dhtml += "<tr height=5px><td></td></tr>";
dhtml += "<td class=/"hotlink/" ><li><a href=/"../../News/newsDetail.aspx?id=" + dt.Rows[i]["articleid"].ToString() + "/" target=/"_blank/" class=/"hotlink/" title=/"" + dt.Rows[i]["title"].ToString() + "/">" + cription + "</a></td>";
}
}
dhtml += "</tr></table></body></html>";
string Filename = "news.html";//DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";//动态的文件名
string Filepath = Server.MapPath(@"/aa/");
//string Cname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
string Filecreatepath = Filepath;//页面要保存的路径+ Cname
string FileFullPath = Filecreatepath + Filename;
//动态生成的静态页面按年月保存本年本月的文件夹中
if (File.Exists(FileFullPath)) //判断当月的文件夹是否存在,
{
//****删除文件
File.Delete(FileFullPath);
DirectoryInfo di = Directory.CreateDirectory(Filecreatepath);
Create_html(FileFullPath, dhtml);
//调用创建html静态页面方法
// Create_html(Filecreatepath + Filename, dhtml);
}
else
{
//创建页面保存到的文件夹
DirectoryInfo di = Directory.CreateDirectory(Filecreatepath);
Create_html(Filecreatepath + Filename, dhtml);
}
}
catch (IOException ex)
{
throw ex;
}
}
private void Create_html(string allfilename, string htmlcode)
{
FileStream CreateFile = new FileStream(allfilename, FileMode.CreateNew);
StreamWriter sw = new StreamWriter(CreateFile);
sw.WriteLine(htmlcode);//将拼好的Html代码写入页面中
sw.Close();
Page.RegisterStartupScript("", "<script>window.alert('The file created successfully.!')</script>");
}
在调用时:
<iframe src="newsTemp/news.html" frameborder=0 width=100%></iframe>