时间:2016-02-16 02:37 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net生成静态页并分页+ubb第1/2页】,下面是详细的讲解!
asp.net生成静态页并分页+ubb第1/2页
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;
}
}
<!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>
关于asp.net生成静态页并分页+ubb第1/2页的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【ASP】ASP.NET中内嵌页面代码的一个问题-NET-内
- 【As】Asp.net中的页面乱码的问题-sp--pn-ne-et
- 【增加记录】asp.net中获取新增加记录的ID Access
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【404页面】ASP.NET设置404页面返回302HTTP状态码
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
