在使用了UEditor後,有時需要編輯舊文章,查看官方文檔,有輸出文章內容,但沒有提交方法,於是參照編輯新文章時的思路,想出了下面的方法:
一、後台定義變量
後台定義一個變量,用於表示舊文章內容:
[csharp]
public string words = "";
public string words = "";
二、導入js和css文件
[javascript]
<script type="text/javascript" src="../../ueditor/editor_config.js"></script>
<script type="text/javascript" src="../../ueditor/editor_all.js"></script>
<script type="text/javascript" src="../../ueditor/editor_config.js"></script>
<script type="text/javascript" src="../../ueditor/editor_all.js"></script>[css] view plaincopyprint?<link rel="Stylesheet" type="text/css" href="../../ueditor/themes/default/css/ueditor.css" />
<link rel="Stylesheet" type="text/css" href="../../ueditor/themes/default/css/ueditor.css" />三、頁面使用
[javascript]
<script type="text/javascript">
var editor = new UE.ui.Editor();
editor.render("editor");
</script>
<script type="text/plain" id="editor" name="myEditor">
<%=words %>
</script>
<script type="text/javascript">
var editor = new UE.ui.Editor();
editor.render("editor");
</script>
<script type="text/plain" id="editor" name="myEditor">
<%=words %>
</script>[csharp]
<div>
<asp:ImageButton ID="imgSubmit" runat="server" ImageUrl="~/images/submit.png" OnClick="imgSubmit_Click" />
</div>
<div>
<asp:ImageButton ID="imgSubmit" runat="server" ImageUrl="~/images/submit.png" OnClick="imgSubmit_Click" />
</div>
四、後台接收
[csharp]
DataClassesDataContext system = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["SystemConnectionString"].ConnectionString.ToString());
DataClassesDataContext system = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["SystemConnectionString"].ConnectionString.ToString());[csharp]
protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
{
aboutus ab = system.aboutus.FirstOrDefault();
string workContent = HttpUtility.HtmlDecode(Request.Form["myEditor"].ToString());//內容
//workContent = Server.HtmlEncode(workContent);//對Html標簽轉碼,但輸出時失去了樣式
string sOrginial = "<script type=\"text/javascript\">";
string sTo = "<script type="text/javascript">";
workContent = workContent.Replace(sOrginial, sTo);//這兩步為了原樣輸出js代碼
string sTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
ab.words = workContent;
ab.updateTime = sTime;
try
{
system.SubmitChanges();
ClientScript.RegisterStartupScript(this.GetType(), "error", "$(function(){asyncbox.alert('更新成功','提示');});window.setTimeout(function(){window.location.href='detail.aspx'},500);", true);
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "error", "$(function(){asyncbox.alert('更新失敗,請稍後重試!','提示');});window.setTimeout(function(){window.location.href='detail.aspx'},500);", true);
}
protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
{
aboutus ab = system.aboutus.FirstOrDefault();
string workContent = HttpUtility.HtmlDecode(Request.Form["myEditor"].ToString());//內容
//workContent = Server.HtmlEncode(workContent);//對Html標簽轉碼,但輸出時失去了樣式
string sOrginial = "<script type=\"text/javascript\">";
string sTo = "<script type="text/javascript">";
workContent = workContent.Replace(sOrginial, sTo);//這兩步為了原樣輸出js代碼
string sTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
ab.words = workContent;
ab.updateTime = sTime;
try
{
system.SubmitChanges();
ClientScript.RegisterStartupScript(this.GetType(), "error", "$(function(){asyncbox.alert('更新成功','提示');});window.setTimeout(function(){window.location.href='detail.aspx'},500);", true);
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "error", "$(function(){asyncbox.alert('更新失敗,請稍後重試!','提示');});window.setTimeout(function(){window.location.href='detail.aspx'},500);", true);
}
其中,asyncbox.alert('','');是jQuery的一個異步彈出框插件