在asp.net中KindEditor编辑器的使用方法小结
作者:
由于国外的服务器好象对一些要引用dll编辑器由于安全问题,锁定了web.config中的一些权限,在先试了FreeTexbox不行,FCKEditor也不行,因为都是要引用dll文件,最后同事介绍一款 纯js的kindeditor编辑器,
下载下来可是不会用啊,网上也找不到类似的方法,可能都没遇到过这样的问题,,经过一个晚上的研究demo及同事一起帮忙,终于研究出了如何使用,自己总结一下,也希望对以后需要的人有所帮助.这里以一个从数据库读取和保存为例子,其它参数请参考kindeditor官方网站
1.首先把下面拷到要用编辑器的路径
<input type="hidden" name="content1" id="content1" value='<% = databind %>'/>
<input type="hidden" name="content" runat="server" id="content"/>
<script type="text/javascript" src="KindEditor.js"></script>
<script type="text/javascript">
document.getElementById("content").value=document.getElementById("content1").value; //这句是因为不能直接把content做为服务器控件才用的,也就是不需要使用<%=this.Content.ClientID%>的,那样数据读不出来,
var editor = new KindEditor("editor");
editor.hiddenName = "content"; //这里是具有Runat="server"属性的input隐藏框名称
editor.editorWidth = "100%";
editor.editorHeight = "280px";
editor.show();
function KindSubmit() {
editor.data();
}
</script>
2.保存按钮
<asp:Button ID="CreateAdmine" runat="server" Height="22" Text="保 存" Width="42" OnClientClick="KindSubmit()" OnClick="CreateAdmine_Click" /> //要客户端提交才能保存
3.后台读取
Aspx页:
<input type="hidden" name="content" id = "content" value='<%=EditorValue %>' /> //这里要用<% =变量 %> 读取服务器端EditorValue变量的值为编辑器初始化内容
<input type="hidden" name="contents" runat="server" id="contents"/>
<script type="text/javascript" src="/editor/KindEditor.js"></script>
<script type="text/javascript">
//document.getElementById("<%=this.contents.ClientID %>").value = document.getElementById("content").value;
document.getElementById("contents").value = document.getElementById("content").value;
var editor = new KindEditor("editor");
editor.hiddenName = "contents";
editor.skinPath = "/editor/skins/default/";
editor.iconPath = "/editor/icons/";
editor.imageAttachPath = "/editor/attached/";
editor.imageUploadCgi = "/editor/upload_cgi/upload.aspx";
editor.cssPath = "/editor/common.css";
editor.editorType = "simple";
editor.editorWidth = "500px";
editor.editorHeight = "300px";
editor.show();
function KindSubmit()
{
editor.data();
}
</script>
CS代码:
protected string EditorValue; //定义一个变量,客户端读取这个变量的值赋给编辑器
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
private void BindData()
{
string sql = "select Content from About where id=1";
DataBase db = new DataBase();
SqlDataReader dr = db.ReturnDataReader(sql);
try
{
if (dr.Read())
{
EditorValue = dr["Content"].ToString().Trim(); //在这里给它赋初始内容
}
}
catch (Exception msg)
{
Response.Write(msg.Message);
}
finally
{
db.Close();
}
}
4.保存的值
Name = content.Value;
1.首先把下面拷到要用编辑器的路径
复制代码 代码如下:
<input type="hidden" name="content1" id="content1" value='<% = databind %>'/>
<input type="hidden" name="content" runat="server" id="content"/>
<script type="text/javascript" src="KindEditor.js"></script>
<script type="text/javascript">
document.getElementById("content").value=document.getElementById("content1").value; //这句是因为不能直接把content做为服务器控件才用的,也就是不需要使用<%=this.Content.ClientID%>的,那样数据读不出来,
var editor = new KindEditor("editor");
editor.hiddenName = "content"; //这里是具有Runat="server"属性的input隐藏框名称
editor.editorWidth = "100%";
editor.editorHeight = "280px";
editor.show();
function KindSubmit() {
editor.data();
}
</script>
2.保存按钮
复制代码 代码如下:
<asp:Button ID="CreateAdmine" runat="server" Height="22" Text="保 存" Width="42" OnClientClick="KindSubmit()" OnClick="CreateAdmine_Click" /> //要客户端提交才能保存
3.后台读取
Aspx页:
复制代码 代码如下:
<input type="hidden" name="content" id = "content" value='<%=EditorValue %>' /> //这里要用<% =变量 %> 读取服务器端EditorValue变量的值为编辑器初始化内容
<input type="hidden" name="contents" runat="server" id="contents"/>
<script type="text/javascript" src="/editor/KindEditor.js"></script>
<script type="text/javascript">
//document.getElementById("<%=this.contents.ClientID %>").value = document.getElementById("content").value;
document.getElementById("contents").value = document.getElementById("content").value;
var editor = new KindEditor("editor");
editor.hiddenName = "contents";
editor.skinPath = "/editor/skins/default/";
editor.iconPath = "/editor/icons/";
editor.imageAttachPath = "/editor/attached/";
editor.imageUploadCgi = "/editor/upload_cgi/upload.aspx";
editor.cssPath = "/editor/common.css";
editor.editorType = "simple";
editor.editorWidth = "500px";
editor.editorHeight = "300px";
editor.show();
function KindSubmit()
{
editor.data();
}
</script>
CS代码:
复制代码 代码如下:
protected string EditorValue; //定义一个变量,客户端读取这个变量的值赋给编辑器
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
private void BindData()
{
string sql = "select Content from About where id=1";
DataBase db = new DataBase();
SqlDataReader dr = db.ReturnDataReader(sql);
try
{
if (dr.Read())
{
EditorValue = dr["Content"].ToString().Trim(); //在这里给它赋初始内容
}
}
catch (Exception msg)
{
Response.Write(msg.Message);
}
finally
{
db.Close();
}
}
4.保存的值
复制代码 代码如下:
Name = content.Value;