将CKEditor集成到ASP.NET中

时间:2012-03-15 03:53:15

标签: javascript asp.net javascript-events ckeditor ckeditor.net

我目前正致力于为网站实施CMS。我使用javascript,jquery,C#等经验很少。我主要处理Java,SQL和C ++。我的问题是我在页面上加载了CKEditor实例。我能够将存储在我的数据库中的HTML加载到CKEditor窗口中,但是我无法从CKEditor中获取已更改的值。

Default.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="CKEditor.NET" Namespace="CKEditor.NET" TagPrefix="CKEditor" %>

<asp:Content ID="Head1" runat="server" ContentPlaceHolderID="head" >
<%--Javascript funciton for Display Contents button--%>
<script type="text/javascript">
    function GetContents() {
        // Display the value of CKEditor into an alert
        alert(CKEDITOR.instances.CKEditor1.getData());
        //Have also tried alert(CKEDITOR.instances[CKEditor1].getData());
    }
</script>
</asp:Content>
<asp:Content ID="form1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<CKEditor:CKEditorControl ID="CKEditor1" runat="server">
</CKEditor:CKEditorControl>
<%--Button that executes the command to store updated data into database--%>
<asp:Button ID="SaveButton" runat="server" Text="Save Changes" 
    onclick="SaveButton_Click" />
<button type="button" onclick="GetContents()">Display Contents</button>

</asp:Content>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Retrieve HTML
        HomePageHTML hp = HomePageHTMLAccess.GetHomPageHTML();
        //Does HTML exist?
        if (hp.HTML != null)
        {
            PopulateControls(hp);
        }
     }

    //Method to load html from database into webpage
    private void PopulateControls(HomePageHTML hp)
    {
        //Display html
        CKEditor1.Text = hp.HTML;

    }
    //Method to save the updated html into the database
    protected void SaveButton_Click(object sender, EventArgs e)
    {
        string text1 = CKEditor1.Text;
        HomePageHTMLAccess.UpdateHomePageHTML(text1);
    }
}

我已经测试并知道我从SaveButton_Click方法写入数据库。我注意到的一件事是我可以显示静态警报消息,例如警报(“消息”);但是我的代码中的任何一行都没有弹出警报窗口。 任何有关设置的帮助,以便我可以使用类结构写入我的数据库,或只是让GetContents()工作将非常感激。

1 个答案:

答案 0 :(得分:1)

在Page_Load中,您应该在编写if (!Page.IsPostBack)控件的Text属性之前检查CKEditor,否则,在每个帖子后面(即按钮单击),控件将具有相同的数据库的价值。 -

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Retrieve HTML
            HomePageHTML hp = HomePageHTMLAccess.GetHomPageHTML();
           //Does HTML exist?
           if (hp.HTML != null)
           {
              PopulateControls(hp);
           }
        }
     }
相关问题