1
我目前正在爲網站實施CMS。我使用javascript,jquery,C#等方面的經驗非常少。我主要處理Java,SQL和C++。我的問題是我有CKEditor實例加載在頁面上。我能夠將我存儲在我的數據庫中的HTML加載到CKEditor窗口中,但是我無法獲得我從CKEditor中更改的值。將CKEditor集成到ASP.NET中
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方法的數據庫。我注意到的一件事是,我可以顯示靜態警報消息,例如alert(「message」);但是我的代碼中沒有任何一行會彈出警告窗口。 任何幫助完成此設置,以便我可以使用類結構寫入我的數據庫,或只是讓GetContents()工作,將不勝感激。
非常感謝你阿德里安,它像一個魅力工作。如果其他人對SaveButton_Click方法有這個問題,請添加一個if(Page.IsPostBack),但不帶感嘆號,並關閉其中的所有代碼,它將起作用。 – tuckerjt07 2012-03-15 17:26:27
@ tuckerjt07,在SaveButton_Click上,Page.IsPostBack將始終爲真(通過按鈕單擊回傳頁面,這是否合理?)。另外,請接受答案。 – 2012-03-15 17:59:58
這就是我的想法,但由於某種原因,直到我在那裏放置if語句纔會起作用。 – tuckerjt07 2012-03-16 05:48:05