首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> ASP.NET >> 正文

asp.net2.0 动态生成静态页并分页实例

    看了看很多网页中的新闻都是分页的,我想实现这样的功能,看看他们实现的方法,基本是一个新闻分成多个html文件保存,然后用页码连接起来。(不知道我的理解是否正确,如果不正确请大家给我指点一二)
1、从数据库中获取新闻内容。包括新闻标题、内容等。
2、设置页面显示的字符串长度,获取新闻内容的字符长度。做初始设置
3、按页面大小设置的长度截取新闻内容的字符。
4、获取模板页面。在相关的替换字符中用标题、内容等替换字符串。
5、保存新页面到制定目录下面

下面是代码和资源管理器内容、


solid.aspx内容
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class solid : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        WriteFile(
"奥运专题""奥运会真棒!<br/>""2""2.html");
    }
    
public void WriteFile(string strText, string content, string page, string name)
    {
        
string path = HttpContext.Current.Server.MapPath("news/get");
        System.Text.Encoding code 
= System.Text.Encoding.GetEncoding("gb2312");

        
// 读取模板文件
        string temp = HttpContext.Current.Server.MapPath("news/text.htm");
        StreamReader sr 
= null;
        StreamWriter sw 
= null;
        
string str = "";
        
try
        {
            sr 
= new StreamReader(temp, code);
            str 
= sr.ReadToEnd(); // 读取文件
        }
        
catch (Exception exp)
        {
            HttpContext.Current.Response.Write(exp.Message);
            HttpContext.Current.Response.End();
            sr.Close();
        }
        
string htmlfilename = name.ToString();
        
// 替换内容
        
// 这时,模板文件已经读入到名称为str的变量中了
        str = str.Replace("$title", strText); //模板页中的$title,即标题
        str = str.Replace("$content", content);//模板页中的$content,即内容
        str = str.Replace("$page", page);//模板页中的$page,即页码连接格式

        
// 写文件
        try
        {
            sw 
= new StreamWriter(path + "/" + htmlfilename, false, code);
            sw.Write(str);
            sw.Flush();
        }
        
catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
        }
        
finally
        {
            sw.Close();
        }
    }
}

 

[1] [2] 下一页

【责编:Luzi】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 社区讨论
 博客论点
 Dotnet频道相关导航