//生成靜態網頁
string path = Server
MapPath(
~/news/
+ folder +
/
)
string file_template_name = Server
MapPath(
~/news/
)
//新聞模版文件
string file_template_content =
;
StreamReader sr_reader = new StreamReader(file_template_name
Encoding
GetEncoding(
gb
))
file_template_content = sr_reader
ReadToEnd()
sr_reader
Close()
string[] subContent = FileSplit(content)
string file_content =
;
int pageNum =
;
while (pageNum <
&& subContent[pageNum] !=
&& subContent[pageNum] != null)
pageNum++;
for (int index =
; 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 =
;
//生成頁碼
if (index >
) firstPage = index
;
for (int i
= firstPage; i
< index +
&& i
< pageNum; i
++)
{
if (i
== index)
pageLink = pageLink +
[
+ (index +
) +
]
+
;
else pageLink = pageLink +
<a + htmlfilename + i
+ >[
+ (i
+
) +
]</a>
;
}
if (index < pageNum
)
pageLink = pageLink +
<a + htmlfilename + (index +
) + >
+
下一頁
+
</a>
;
if (index >
)
pageLink =
<a + htmlfilename + (index
) + >
+
上一頁
+
</a>
+ pageLink;
file_content = file_content
Replace(
<!
$$pageLink
>
pageLink)
if (index == pageNum
&&attachment_filename != null && attachment_filename != String
Empty)
{
string[] attachment = attachment_filename
Split(new char[] {
|
})
string attachmenthtml =
<a + attachment[
] +
>
+ attachment[
] +
</a>
;
for (int j =
; j < attachment
Length; j++)
attachmenthtml = attachmenthtml +
<br/> <a + attachment[j] +
>
+ attachment[j] +
</a>
;
file_content = file_content
Replace(
$$attachment
attachmenthtml)
}
StreamWriter sw = new StreamWriter(path + htmlfilename + index +
false
Encoding
GetEncoding(
gb
))
sw
Write(file_content)
sw
Flush()
sw
Close()
}
HyperLink
Text =
預覽
+ title;
HyperLink
NavigateUrl =
/news/
+ folder +
/
+ htmlfilename +
+
;
HyperLink
Visible = true;
txt_time
Text = DateTime
Now
ToString(
yyyy
MM
dd
)
txt_title
Text =
;
txt_author
Text =
;
FreeTextBox
Text =
;
}
catch (Exception e)
{
}
}
//將正文分成多個頁面
protected string[] FileSplit(string fileContent)
{
int fileIndex =
;
string[] splitedFile = new string[
];
while (fileContent
Length >
&& fileIndex <
) //每頁至少
個字符
{
if (fileContent
IndexOf(
<P>
) <
) break;
splitedFile[fileIndex] = fileContent
Substring(
fileContent
IndexOf(
<P>
))
fileContent = fileContent
Remove(
splitedFile[fileIndex]
Length)
fileIndex++;
}
splitedFile[fileIndex] = fileContent; //超過
頁
剩下部分全放第十頁
return splitedFile;
}
}
From:http://tw.wingwit.com/Article/program/net/201311/13952.html