ASP.NET实现新闻页面的分页功能[生成静态版]

//生成静态网页

             string path = Server.MapPath("~/news/" + folder + "/");

             string file_template_name = Server.MapPath("~/news/template.htm");    //新闻模版文件
             string file_template_content = "";
             StreamReader sr_reader = new StreamReader(file_template_name, Encoding.GetEncoding("gb2312"));
             file_template_content = sr_reader.ReadToEnd();
             sr_reader.Close();
             string[] subContent = FileSplit(content);
             string file_content = "";
             int pageNum = 0;
             while (pageNum < 10 && subContent[pageNum] != "" && subContent[pageNum] != null)
                 pageNum++;
             for (int index = 0; index < pageNum; index++)
             ...{
                 file_content = file_template_content;
                 file_content = file_content.Replace("$$category", list_department.SelectedItem.Text);     //新闻类别
                 file_content = file_content.Replace("$$title", title);                //新闻标题
                 file_content = file_content.Replace("<!--来源:$$author-->", "来源:" + author);         //作者
                 file_content = file_content.Replace("$$time", time);                      //添加时间
                 file_content = file_content.Replace("$$content", subContent[index]);        //新闻正文
                 string pageLink = "";
                 int firstPage = 0;
                //生成页码
                 if (index > 2) firstPage = index - 2;
                 for (int i1 = firstPage; i1 < index + 3 && i1 < pageNum; i1++)
                 ...{
                     if (i1 == index)
                         pageLink = pageLink + "[" + (index + 1) + "]" + "&nbsp; &nbsp; &nbsp; &nbsp;";
                     else pageLink = pageLink + "<a href=" + htmlfilename + i1 + ".htm>[" + (i1 + 1) + "]</a>&nbsp; &nbsp; &nbsp; &nbsp;";

                 }
                 if (index < pageNum - 1)
                     pageLink = pageLink + "<a href=" + htmlfilename + (index + 1) + ".htm>" + "下一页" + "</a>&nbsp; &nbsp; &nbsp; &nbsp;";
                 if (index > 0)
                     pageLink = "<a href=" + htmlfilename + (index - 1) + ".htm>" + "上一页" + "</a>&nbsp; &nbsp; &nbsp; &nbsp;" + pageLink;
                 file_content = file_content.Replace("<!-- $$pageLink -->", pageLink);
                 if (index == pageNum - 1&&attachment_filename != null && attachment_filename != String.Empty)
                 ...{
                     string[] attachment = attachment_filename.Split(new char[] ...{ '|' });
                     string attachmenthtml = "<a href="attachment/" + attachment[0] + "" >" + attachment[0] + "</a>";
                     for (int j = 1; j < attachment.Length; j++)
                         attachmenthtml = attachmenthtml + "<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="attachment/" + attachment[j] + "" >" + attachment[j] + "</a>";
                     file_content = file_content.Replace("$$attachment", attachmenthtml);
                 }
                 StreamWriter sw = new StreamWriter(path + htmlfilename + index + ".htm", false, Encoding.GetEncoding("gb2312"));
                 sw.Write(file_content);
                 sw.Flush();
                 sw.Close();
             }
             HyperLink1.Text = "预览: " + title;
             HyperLink1.NavigateUrl = "../news/" + folder + "/" + htmlfilename   + "0" + ".htm";
             HyperLink1.Visible = true;
             txt_time.Text = DateTime.Now.ToString("yyyy-MM-dd");
             txt_title.Text = "";
             txt_author.Text = "";
             FreeTextBox1.Text = "";
         }
         catch (Exception e)
         ...{
         }
     }
    
     //将正文分成多个页面
protected string[] FileSplit(string fileContent)
     ...{
         int fileIndex = 0;
         string[] splitedFile = new string[10];
         while (fileContent.Length > 1500 && fileIndex < 9)     //每页至少1500个字符
         ...{
             if (fileContent.IndexOf("<P>", 1500) < 0) break;
             splitedFile[fileIndex] = fileContent.Substring(0, fileContent.IndexOf("<P>", 1500));
             fileContent = fileContent.Remove(0, splitedFile[fileIndex].Length);
             fileIndex++;
         }
         splitedFile[fileIndex] = fileContent;    //超过9页,剩下部分全放第十页
         return splitedFile;
     }
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET实现分页功能,一般需要以下步骤: 1. 获取数据:从数据库或其他数据源中获取需要分页的数据。 2. 计算总页数:根据每页显示的记录数和总记录数计算出总页数。 3. 显示分页导航:根据当前页和总页数,生成分页导航,让用户能够跳转到不同的页面。 4. 显示当前页数据:根据当前页码和每页显示的记录数,从数据中获取对应的数据进行显示。 下面是一个简单的示例代码: 1. 获取数据 ```csharp int pageIndex = 1; // 当前页码 int pageSize = 10; // 每页显示的记录数 var data = GetDataFromDatabase(); // 从数据库中获取数据 int totalCount = data.Count(); // 记录总数 // 计算总页数 int totalPages = (int)Math.Ceiling((double)totalCount / pageSize); ``` 2. 显示分页导航 ```csharp for (int i = 1; i <= totalPages; i++) { // 根据当前页码和总页数生成分页导航链接 string link = string.Format("/List.aspx?page={0}", i); if (i == pageIndex) { // 当前页码不需要链接,直接显示文本 Response.Write(i); } else { // 显示链接 Response.Write(string.Format("<a href='{0}'>{1}</a>", link, i)); } } ``` 3. 显示当前页数据 ```csharp var pageData = data.Skip((pageIndex - 1) * pageSize).Take(pageSize); foreach (var item in pageData) { // 显示数据 Response.Write(item); } ``` 当然,这只是一个简单的示例,实际情况可能会更加复杂,需要根据具体需求进行调整。但是以上步骤可以作为一个基本框架,供你参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值