<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false" CodeBehind="ueditor_1_2.aspx.cs" Inherits="UEditor.ueditor_1_2" %>
<!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>百度ueditor编辑器在Asp.Net中使用</title>
<script src="ueditor1.2.0/editor_config.js" type="text/javascript"></script>
<script src="ueditor1.2.0/editor_all.js" type="text/javascript"></script>
<link href="ueditor1.2.0/themes/default/ueditor.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="myForm" runat="server" method="post">
<div>
<!--<div id="myEditor"></div>-->
<!--赋初值-->
<script type="text/plain" id="myEditor">初始内容</script>
<!--隐藏控件,由Asp.Net赋值(临时存放)-->
<div id="HiddenDiv" runat="server" style="display:none;"></div>
<script type="text/javascript">
var URL = "./ueditor/"; //这里你可以配置成ueditor目录在您网站的相对路径或者绝对路径
var editor = new baidu.editor.ui.Editor();
editor.render("myEditor");
editor.setHeight(100); //设置编辑器的高度
//editor.setContent("为编辑器设置初值");
editor.setContent(document.getElementById("<%=this.HiddenDiv.ClientID%>").innerHTML);//从隐藏控件中取出临时数据
</script>
</div>
<asp:Button ID="Button1" runat="server" Text="服务器控件" />
<input id="Button2" οnclick="submitData()" type="button" value="HTML控件" /></form>
<p>
</p>
<p>
</p>
</body>
</html>
<script type="text/javascript">
function submitData() {
if (editor.hasContents()) { //提交条件满足时提交内容
editor.sync(); //此处的editor是页面实例化出来的编辑器对象
var v = editor.getContent();
alert(v);
document.getElementById('myForm').submit();
}
}
</script>
//------------------------------.cs部分--------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace UEditor
{
public partial class ueditor_1_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params["editorValue"] != null)
{
string content = Request.Params["editorValue"];//获取编辑器的内容
this.HiddenDiv.InnerHtml = content;//保证回发不丢值
}
else
{
this.HiddenDiv.InnerHtml = "服务器端加载的初始值";
}
}
}
}
//------------------------------上传图片配置-----------------------------
修改配置文件editor_config.js中的
imagePath属性可以为空,更换上传文件路径需修改dialogs/image/image.html文件,如图:
//--------------------------FilesUpload.ashx文件代码-------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UEditor.Ajax
{
/// <summary>
/// FilesUpload 的摘要说明
/// </summary>
public class FilesUpload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
///context.Response.Write("Hello World");
string state = "SUCCESS";
HttpFileCollection files=context.Request.Files;
if (files.Count > 0)
{
string rootPath = context.Request.MapPath("~/") + "UploadFiles/";
string url = String.Empty;
for (int i = 0; i < files.Count;i++ )
{
try
{
files[i].SaveAs(rootPath + files[i].FileName);
//url = "/UploadFiles/" + files[i].FileName;
url = files[i].FileName;//在editor_config.js文件中配置有根目录路径
}
catch
{
state = "ERROR";
}
}
HttpContext.Current.Response.Write("{'url':'" + url + "','title':'" + context.Request.Params["pictitle"] + "','state':'" + state + "'}");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}