亲宝软件园·资讯

展开

asp.net生成静态页 asp.net生成静态页并分页+ubb

人气:0
想了解asp.net生成静态页并分页+ubb的相关内容吗,在本文为您仔细讲解asp.net生成静态页的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:asp.net,生成静态页,下面大家一起来学习吧。


作为练习我加入了“插入代码”“插入运行代码”,大家可以看情况,加入其他UBB。

2、代码:
default.aspx.cs

复制代码 代码如下:

using System;
using System.Data;
using System.Configuration;
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.Text.RegularExpressions;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnOk_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/template/news.htm");
string toPath = Server.MapPath("~/news/");
string[] tempContent = new string[] { };
string s1 = ReadFile(path);
string title = this.tbxTitle.Text;
string content = UBB(this.tbxContent.Text);
string s3 = string.Empty;
content = content.Replace("[ page]","¤");
tempContent = content.Split('¤');
string pageNav = string.Empty;
for (int j = 0; j < tempContent.Length; j++)
{
pageNav += "<a href='" + (j + 1) + ".htm'>第" +(j + 1) + "页</a> ";
}
for (int i = 0; i < tempContent.Length; i++)
{
s3 = s1.Replace("$title$", title);
s3 = s3.Replace("$content$", tempContent[i].ToString());
s3 = s3.Replace("$pagelist$",pageNav);
WriteFile(toPath + (i + 1) + ".htm", s3);
s3 = string.Empty;
}
}

//ubb替换
public String UBB(string sDetail)
{
sDetail = Server.HtmlEncode(sDetail);
sDetail = sDetail.Replace("\r\n","<br />");
Regex r;
Match m;
//code
r = new Regex(@"(\[code\])([\s\S]+?)(\[\/code\])", RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(), "<textarea style=\"border:1px solid #94BBE2;background:#FAFAFA;width:90%;cursor:default;padding:5px;\" rows=\"15\" >" + m.Groups[2].ToString().Replace("<br />","\n")+ "</textarea><br />");
}

int i = 1;
//html
r = new Regex(@"(\[html\])([\s\S]+?)(\[\/html\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
i = i + 1;
sDetail = sDetail.Replace(m.Groups[0].ToString(),"<textarea rows=\"12\" style=\"width:90%\" id=\"code" + i + "\">" + m.Groups[2].ToString().Replace("<br />","\n") + "</textarea><br /><input type='button' value='运行代码' onclick=\"runCode('code" +i+ "')\" /><input type='button' value='复制代码' onclick=\"copyCode('code" +i+ "')\" />");
}
return sDetail;
}

//写文件
public static void WriteFile(string Path, string Strings)
{
if (!System.IO.File.Exists(Path))
{
System.IO.FileStream f = System.IO.File.Create(Path);
f.Close();
}
System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, false, System.Text.Encoding.GetEncoding("utf-8"));
f2.Write(Strings);
f2.Close();
f2.Dispose();
}

//读文件
public static string ReadFile(string Path)
{
string s = "";
if (!System.IO.File.Exists(Path))
s = "不存在相应的目录";
else
{
StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("utf-8"));
s = f2.ReadToEnd();
f2.Close();
f2.Dispose();
}
return s;
}

}


default.aspx
复制代码 代码如下:

<%@ Page Language="C#" AutoEventWireup="true" validateRequest="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>生成静态页并分页</title>
<style type="text/css">
*{
font-size:12px;
}
#menu{
padding:0;
margin:0;
}
#menu li{
list-style-type:none;
float:left;
margin-right:10px;
}
.myTable,.myTable td{
border:1px solid #cccccc;
border-collapse:collapse;
}
</style>
<script language="javascript" type="text/javascript">
//js代码部分来自“天下无双”网友
function AddText(NewCode){
setfocus();
var edit = document.selection.createRange();
if(edit){
if(edit.text.length > 0){
edit.text += NewCode;
}else{
edit.text = NewCode;
}
edit.select();
}
}

function setfocus(){
getinput().focus();
}

function getinput(){
return document.getElementById("tbxContent");
}

function code(){
addText = "\r[ code]\r
";
AddText(addText);
}

function html(){
addText = "\r";
AddText(addText);
}

function page(){
addText = "[ page]";
AddText(addText);
}
function runCode(cod1){cod=document.getElementById(cod1);var codcode=cod.value;if(code!=""){var newwin=window.open('','','');newwin.opener=null;newwin.document.write(code);newwin.document.close();}}
function copyCode(obj){var temp=document.getElementById(obj);if(document.all){var rng=document.body.createTextRange();rng.moveToElementText(temp);rng.scrollIntoView();rng.select();rng.execCommand("Copy");rng.collapse(false);}else
{window.alert("此功能仅在IE上有效");}}
function saveCode(obj){var winname=window.open('','_blank','top=10000');winname.document.open('text/html','replace');winname.document.write(obj.value);winname.document.execCommand('saveas','','code.htm');winname.close();}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="myTable">
<tr>
<td>标题:</td>
<td style="width: 478px"><asp:TextBox ID="tbxTitle" runat="server" Width="277px"></asp:TextBox></td>
</tr>
<tr>
<td style="height: 347px">内容:</td>
<td style="width: 478px; height: 347px">
<ul id="menu">
<li><a href="javascript:page()">[插入分页]</a></li>
<li><a href="javascript:code()">[插入代码]</a></li>
<li><a href="javascript:html()">[插入可运行代码]</a></li>
</ul>
<asp:TextBox ID="tbxContent" runat="server" Height="296px" TextMode="MultiLine" Width="469px"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnOk" runat="server" Text="生成静态页并分页" OnClick="btnOk_Click" />
</td>
</tr>
</table>

</div>
<asp:Label ID="lbl" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
[/code]
news.htm
复制代码 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>$title$</title>
<script language="javascript" type="text/javascript">
function runCode(cod1){cod=document.getElementById(cod1);var codcode=cod.value;if(code!=""){var newwin=window.open('','','');newwin.opener=null;newwin.document.write(code);newwin.document.close();}}
function copycode(obj){var temp=document.getElementById(obj);if(document.all){var rng=document.body.createTextRange();rng.moveToElementText(temp);rng.scrollIntoView();rng.select();rng.execCommand("Copy");rng.collapse(false);}else
{window.alert("此功能仅在IE上有效");}}
//高亮当前页
window.onload = function(){
var pagelist = document.getElementById("pagelist").getElementsByTagName("a");
for(var i = 0;i < pagelist.length;i++){
var links = pagelist[i].getAttribute("href");
var myURL = document.location.href;
if(myURL.indexOf(links) != -1){
pagelist[i].className = "D";
}
}
}
</script>
<style type="text/css">
body{
text-align:center;
}
h1{
width:100%;
text-align:center;
}
#board{
width:500px;
text-align:left;
}
a{
font-size:12px;
}
a.D:link,a.D:visited{
color:red;
}
</style>
</head>
<body>
<div id="board">
<h1>$title$</h1>
$content$
<div id="pagelist">$pagelist$</div>
</div>

<br />

</body>
</html>

打包下载http://xiazai.jb51.net/200810/yuanma/asp.net_page.rar

加载全部内容

相关教程
猜你喜欢
用户评论