当前位置:   article > 正文

【ASP.NET】5.百度富文本编辑器UEditor之存入数据库_ueditor内容保存到数据库

ueditor内容保存到数据库

 最近项目要实现一个新闻发布的功能,要实现图文混排存储入数据库,再取出来进行页面展示的效果,于是网上搜了一个资料,在这里做一下总结,百度富文本编辑器有Java,php,.net三个版本,这里我下载的.net版本的,下载地址:http://ueditor.baidu.com/website/download.html

1、下载后要先看一下ueditor.config.js和asp/config.json的文件,里面是关于图片上传和其他方法的一些路径配置:

ueditor.config.js文件:


上传图片路径的配置文件asp/config.json

2、显示编辑页面(一定要看使用文档:http://fex.baidu.com/ueditor/#server-deploy)

(1)引入所需要的js文件

  1. <script type="text/javascript" charset="utf-8" src="ueditor.config.js"></script>
  2. <script type="text/javascript" charset="utf-8" src="ueditor.all.js"> </script>
  3. <!--建议手动加在语言,避免在ie下有时因为加载语言失败导致编辑器加载失败-->
  4. <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型,比如你在配置项目里配置的是英文,这里加载的中文,那最后就是中文-->
  5. <script type="text/javascript" charset="utf-8" src="lang/zh-cn/zh-cn.js"></script>
(2)初始化编辑器,html代码:

  1. <div>
  2. <textarea class="A4page" id="myEditor" name="NewsContent"></textarea>
  3. </div>
JavaScript代码:

  1. <!-- 实例化编辑器 -->
  2. <script type="text/javascript">
  3. var ue = UE.getEditor('myEditor', {
  4. toolbars: [
  5. ['fullscreen', 'source', '|', 'undo', 'redo', '|',
  6. 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
  7. 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
  8. 'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
  9. 'directionalityltr', 'directionalityrtl', 'indent', '|',
  10. 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
  11. 'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
  12. 'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertframe', 'insertcode', 'pagebreak', 'template', 'background', '|',
  13. 'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|',
  14. 'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|',
  15. 'print', 'preview', 'searchreplace', 'help', 'drafts']
  16. ],
  17. allHtmlEnabled: false,//提交到后台的数据是否包含整个html字符串
  18. autoHeightEnabled: false,
  19. autoFloatEnabled: true,
  20. allowDivTransToP: false//阻止div标签自动转换为p标签
  21. });
  22. </script>
说明修改配置项的方法:

方法一:修改ueditor.config.js里面的toolbars

方法二:实例化编辑器的时候传入toolbars参数(详情参考:http://fex.baidu.com/ueditor/#start-toolbar)

3、前端配置基本上配置好了,下面是后端配置(如果后端配置不好,上传图片功能将无法使用),在提交内容的时候我也遇到了一个坑了我一天的问题,由于是.NET的WinForm,点击按钮提交的时候,页面会整个刷新,所以富文本框内输入的值,由于页面的刷新传入后端的会是空值,我这里解决的方法是把富文本编辑器的赋值语句写到一个js的方法里面,然后把值存到一个隐藏域里面,把提交按钮放到UpdatePanel里面,只刷新按钮,不刷新页面其他部分,点击按钮执行提交之前,先执行这个js方法,取到富文本编辑器里面的值以后再执行存储数据库的操作,具体代码如下:

news_publish.aspx

  1. <%@ Page ValidateRequest="false" Title="新闻发布" Language="C#" MasterPageFile="~/LJMB/MB01/MP01.master" AutoEventWireup="true" CodeFile="news_publish.aspx.cs" Inherits="LJWY_MG00_news_publish" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="cphBody" runat="Server">
  3. <script type="text/javascript" charset="utf-8" src="ueditor.config.js"></script>
  4. <script type="text/javascript" charset="utf-8" src="ueditor.all.js"> </script>
  5. <!--建议手动加在语言,避免在ie下有时因为加载语言失败导致编辑器加载失败-->
  6. <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型,比如你在配置项目里配置的是英文,这里加载的中文,那最后就是中文-->
  7. <script type="text/javascript" charset="utf-8" src="lang/zh-cn/zh-cn.js"></script>
  8. <div class="divPaH010"></div>
  9. <%--2. 组合页面按钮--%>
  10. <div class="divPaW080"></div>
  11. <%--</div>--%>
  12. <%--3. 组合数据控件--%>
  13. <div class="divPaH010"></div>
  14. <div class="divMpP302_1">
  15. <div class="divPaH020"></div>
  16. <div class="divPaH020">
  17. <div class="divPaW080">
  18. <asp:Label ID="lblCTOW" runat="server" CssClass="lbtMpL306" Text="新闻标题:"></asp:Label></div>
  19. <div class="divPaW120_1">
  20. <asp:TextBox ID="txtCTOW" runat="server" CssClass="txtMpL301_1" οnmοuseοver="className='txtMpL302_1'" οnmοuseοut="className='txtMpL301_1'"> </asp:TextBox></div>
  21. <div class="divPaW080">
  22. <asp:Label ID="lblCTY2" runat="server" CssClass="lbtMpL306" Text="新闻类别:"></asp:Label></div>
  23. <div class="divPaW120">
  24. <asp:DropDownList ID="ddlCTY2" runat="server" CssClass="txtMpL301"></asp:DropDownList></div>
  25. <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
  26. <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  27. <ContentTemplate>
  28. <div class="divPaW080_1"><asp:LinkButton ID="btnQuery" runat="server" Text="发布" CssClass="lbtMpL305" CommandName="Save" οnmοuseοver="setover(this)" οnmοuseοut="setout(this)" OnClick="btnEvent_Click"></asp:LinkButton></div>
  29. <%--<div class="divPaW080"><asp:LinkButton ID="btnReset" runat="server" Text="重设" CssClass="lbtMpL305" CommandName="Reset" οnmοuseοver="setover(this)" οnmοuseοut="setout(this)" OnClick="btnEvent_Click"></asp:LinkButton></div>--%>
  30. <asp:Label ID="lblWarning" runat="server" CssClass="lblWarning"></asp:Label>
  31. </ContentTemplate>
  32. </asp:UpdatePanel>
  33. </div>
  34. <div class="divPaH020"></div>
  35. <div class="divPaH020">
  36. <%--<div class="divPaW080"><asp:Label ID="lblCTDS" runat="server" CssClass="lbtMpL306" Text="新闻内容"></asp:Label></div><div class="divPaW120"><asp:TextBox ID="txtCITX" TextMode="MultiLine" runat="server" CssClass="txtMpL3011" οnmοuseοver="className='txtMpL3021'" οnmοuseοut="className='txtMpL3011'"> </asp:TextBox></div> --%>
  37. <div style="margin: 0 auto;">
  38. <div>
  39. <textarea class="A4page" id="myEditor" name="NewsContent"></textarea>
  40. </div>
  41. <asp:HiddenField ID="HiddenField1" runat="server" />
  42. <!-- 实例化编辑器 -->
  43. <script type="text/javascript">
  44. var ue = UE.getEditor('myEditor', {
  45. toolbars: [
  46. ['fullscreen', 'source', '|', 'undo', 'redo', '|',
  47. 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
  48. 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
  49. 'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
  50. 'directionalityltr', 'directionalityrtl', 'indent', '|',
  51. 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
  52. 'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
  53. 'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertframe', 'insertcode', 'pagebreak', 'template', 'background', '|',
  54. 'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|',
  55. 'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|',
  56. 'print', 'preview', 'searchreplace', 'help', 'drafts']
  57. ],
  58. allHtmlEnabled: false,//提交到后台的数据是否包含整个html字符串
  59. autoHeightEnabled: false,
  60. autoFloatEnabled: true,
  61. allowDivTransToP: false//阻止div标签自动转换为p标签
  62. });
  63. function UserAddVerify() {
  64. ue.ready(function () {
  65. var html = ue.getContent();
  66. document.getElementById("<%=HiddenField1.ClientID %>").value = html;
  67. console.log(html);
  68. });
  69. }
  70. </script>
  71. </div>
  72. </div>
  73. </div>
  74. </asp:Content>
news_publish.aspx.cs

  1. #region 2. 页面加载
  2. /// <summary>
  3. /// 页面加载事件
  4. /// </summary>
  5. /// <param name="sender"></param>
  6. /// <param name="e"></param>
  7. protected void Page_Load(object sender, EventArgs e)
  8. {
  9. if (!IsPostBack)
  10. {
  11. //给btnQuery添加客户端事件
  12. btnQuery.Attributes.Add("OnClick", "return UserAddVerify()");
  13. //设置菜单编号
  14. pgMenuId = "401101";
  15. PgFnInitial();
  16. }
  17. }
  18. #endregion //2. 页面加载
  19. #region 3. 页面事件
  20. #region 3.1 按钮事件
  21. /// <summary>
  22. /// 按钮事件
  23. /// </summary>
  24. /// <param name="sender"></param>
  25. /// <param name="e"></param>
  26. protected void btnEvent_Click(object sender, EventArgs e)
  27. {
  28. #region 得到事件类别
  29. string strCMNA = "";
  30. strCMNA = ((LinkButton)(sender)).CommandName.ToString();
  31. #endregion 得到事件类别
  32. #region 保存按钮事件
  33. if (strCMNA == "Save")
  34. {
  35. #region 变量定义
  36. string strTableName = "OC11HN";
  37. ArrayList aryInsertFiled = new ArrayList();
  38. ArrayList aryInsertValues = new ArrayList();
  39. string strTableNamehd = "OC11DB";
  40. ArrayList aryInsertFiledhd = new ArrayList();
  41. ArrayList aryInsertValueshd = new ArrayList();
  42. string strSYID = "";
  43. string strCTID = "";
  44. string strCTNA = "";
  45. string strCTFG = "";
  46. string strCTTY = "";
  47. string strCTY1 = "";
  48. string strCTY2 = "";
  49. string strCTDT = "";
  50. string strPBTM = "";
  51. string strCTOW = "";
  52. string strATFG = "";
  53. string strCTIT = "";
  54. string strCIFG = "";
  55. string strCITX = "";
  56. #endregion //变量定义
  57. #region 变量赋值
  58. strSYID = "FRPL";
  59. strCTID = objDrFn.GetFlowNo("FRPL", "XWGL", "XWID");
  60. strCTNA = "二级页面";
  61. strCTFG = "Y";
  62. strCTTY = "精彩剪影";
  63. strCTY1 = "精彩剪影";
  64. strCTY2 = ddlCTY2.SelectedValue.ToString().Trim();
  65. strCTDT = DateTime.Now.ToString("yyyyMMdd");
  66. strPBTM = DateTime.Now.ToString("yyyy-MM-dd");
  67. strATFG = "Y";
  68. strCTOW = txtCTOW.Text.ToString().Trim();
  69. strATFG = "Y";
  70. strCTIT = "001";
  71. strCIFG = "Y";
  72. //strCITX = hidInput.Value;
  73. strCITX = HiddenField1.Value;
  74. #endregion //变量赋值
  75. #region 变量处理
  76. strSYID = objDrFn.FmatStr(strSYID);
  77. strCTID = objDrFn.FmatStr(strCTID);
  78. strCTNA = objDrFn.FmatStr(strCTNA);
  79. strCTFG = objDrFn.FmatStr(strCTFG);
  80. strCTTY = objDrFn.FmatStr(strCTTY);
  81. strCTY1 = objDrFn.FmatStr(strCTY1);
  82. strCTY2 = objDrFn.FmatStr(strCTY2);
  83. strATFG = objDrFn.FmatStr(strATFG);
  84. strCTDT = objDrFn.FmatStr(strCTDT);
  85. strPBTM = objDrFn.FmatStr(strPBTM);
  86. strCTOW = objDrFn.FmatStr(strCTOW);
  87. strATFG = objDrFn.FmatStr(strATFG);
  88. strCTIT = objDrFn.FmatStr(strCTIT);
  89. strCIFG = objDrFn.FmatStr(strCIFG);
  90. strCITX = objDrFn.FmatStr(strCITX);
  91. #endregion //变量处理
  92. #region 变量验证
  93. #region 非空验证
  94. //主键的非空验证
  95. if (strCTOW == "")
  96. {
  97. lblWarning.Text = "新闻标题 不允许为空!";
  98. return;
  99. }
  100. #endregion //非空验证
  101. #region 整型验证
  102. //if (!objDsFt.CheckInt(变量名))
  103. //{
  104. // lblWarning.Text = "变量名 必须是整型数据";
  105. // return;
  106. //}
  107. #endregion //整型验证
  108. #region 浮点验证
  109. //if (!objDsFt.CheckDeciaml(变量名))
  110. //{
  111. // lblWarning.Text = "变量名 必须是数值型数据";
  112. // return;
  113. //}
  114. #endregion //浮点验证
  115. #region 逻辑验证
  116. //string strFileAllPath = fileUplod.PostedFile.FileName.ToString().Trim();
  117. //if (strFileAllPath != "")
  118. //{
  119. // string strSavePath = DsCg.GetUploadPath(Page) + "CPTP\\";
  120. // string strFileName = strURID;// objDsnf.GetFileName(strFileAllPath);
  121. // string strFileExName = objDsFt.GetFileExName(strFileAllPath).ToUpper();
  122. // if ((strFileExName != "JPG") && (strFileExName != "PNG"))
  123. // {
  124. // ClientScript.RegisterStartupScript(typeof(string), "alert", "<script>alert('员工头像只支持JPG、PNG图片,请选择正确的图片!')</script>");
  125. // return;
  126. // }
  127. // string strTime = DateTime.Now.ToString("yyyyMMddHHmmss");
  128. // string strFileNewName = strSavePath + strFileName + "" + strTime + "." + strFileExName;
  129. // fileUplod.PostedFile.SaveAs(strFileNewName);
  130. // strURPC = "../../LJSC/CPTP/" + strFileName + "" + strTime + "." + strFileExName;
  131. //}
  132. //if (string.IsNullOrEmpty(strURPC))
  133. //{
  134. // strURPC = "../../LJTP/default.jpg";
  135. //}
  136. #endregion //逻辑验证
  137. #region 主键验证
  138. #endregion //主键验证
  139. #endregion //变量验证
  140. #region 变量组合
  141. aryInsertFiled.Add("SYID");
  142. aryInsertFiled.Add("CTID");
  143. aryInsertFiled.Add("CTNA");
  144. aryInsertFiled.Add("CTFG");
  145. aryInsertFiled.Add("CTTY");
  146. aryInsertFiled.Add("CTY1");
  147. aryInsertFiled.Add("CTY2");
  148. aryInsertFiled.Add("CTDT");
  149. aryInsertFiled.Add("PBTM");
  150. aryInsertFiled.Add("CTOW");
  151. aryInsertFiled.Add("ATFG");
  152. aryInsertValues.Add(strSYID);
  153. aryInsertValues.Add(strCTID);
  154. aryInsertValues.Add(strCTNA);
  155. aryInsertValues.Add(strCTFG);
  156. aryInsertValues.Add(strCTTY);
  157. aryInsertValues.Add(strCTY1);
  158. aryInsertValues.Add(strCTY2);
  159. aryInsertValues.Add(strCTDT);
  160. aryInsertValues.Add(strPBTM);
  161. aryInsertValues.Add(strCTOW);
  162. aryInsertValues.Add(strATFG);
  163. aryInsertFiledhd.Add("SYID");
  164. aryInsertFiledhd.Add("CTID");
  165. aryInsertFiledhd.Add("CTIT");
  166. aryInsertFiledhd.Add("CIFG");
  167. aryInsertFiledhd.Add("CITX");
  168. aryInsertValueshd.Add(strSYID);
  169. aryInsertValueshd.Add(strCTID);
  170. aryInsertValueshd.Add(strCTIT);
  171. aryInsertValueshd.Add(strCIFG);
  172. aryInsertValueshd.Add(strCITX);
  173. #endregion //变量组合
  174. #region 执行保存
  175. bool bolIfOk = objDrFn.InsertRecode(strTableName, aryInsertFiled, aryInsertValues);
  176. bool bolIfOkhd = objDrFn.InsertRecode(strTableNamehd, aryInsertFiledhd, aryInsertValueshd);
  177. #endregion //执行保存
  178. #region 判断结果
  179. if (!(bolIfOk))
  180. {
  181. lblWarning.Text = "数据保存失败!";
  182. return;
  183. }
  184. #endregion //判断结果
  185. #region 显示结果
  186. //设置保存成功的数据
  187. lblWarning.Text = "数据信息保存成功!";
  188. //PgFnInitial();
  189. objDrFn.UpdateMaxFlowNo("FRPL", "XWGL", "XWID");
  190. //DataTable dt = objDrFn.GetDataTable("SELECT * FROM OC11HN WHERE CTID='" + strCTID + "'");
  191. //gvMain.DataSource = dt;
  192. //gvMain.DataBind();
  193. #endregion //显示结果
  194. }
  195. #endregion //保存按钮事件
  196. #region 重设按钮事件
  197. else if (strCMNA == "Reset")
  198. {
  199. PgFnInitial();
  200. }
  201. #endregion //重设按钮事件
  202. }
  203. #endregion //3.1 按钮事件
  204. #region 4. 页面函数
  205. #region 4.1 控件数据设置
  206. /// <summary>
  207. /// 控件数据设置
  208. /// </summary>
  209. private void PgFnInitial()
  210. {
  211. #region 验证用户权限
  212. //PgFnValUserAur();
  213. #endregion //验证用户权限
  214. #region Set Nomal Component
  215. //gvMain.DataSource = null;
  216. //gvMain.DataBind();
  217. #endregion //Set Nomal Component
  218. //初始化页面控件
  219. //初始化页面控件
  220. txtCTOW.Text = "";
  221. //txtCITX.Text = "";
  222. objDrFn.SetDdl(ddlCTY2, "FRPL", "XWTP", "CLNA", "CLTX");
  223. }
  224. #endregion //4.1 控件数据设置
页面效果如图所示:

会以html的形式存入数据库,如下图所示:






声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/230341?site
推荐阅读
相关标签
  

闽ICP备14008679号