<%@ 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" onclick="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;
}
}
}
}