当前位置:编程学习 > C#/ASP.NET >>

百度UEditor编辑旧文章

在使用了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的一个异步弹出框插件

 

 

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,