ASP.NET實現新聞頁面的分頁功能

通過Freetextbox添加的新聞因爲包含HTML標記,因此在分頁時不能按照字數劃分,而且每一段的內容不便拆開,可以在按字數分段後查找下一個

標記,使得每一段都不至顯示在兩頁中.下面代碼還實現了頁碼的顯示: 

//生成靜態網頁

            
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< 0break;
            splitedFile[fileIndex] 
= fileContent.Substring(0, fileContent.IndexOf("<P>"1500));
            fileContent 
= fileContent.Remove(0, splitedFile[fileIndex].Length);
            fileIndex
++;
        }

        splitedFile[fileIndex] 
= fileContent;   //超過9頁,剩下部分全放第十頁
        return splitedFile;
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章