Domanda

Attualmente sto lavorando per implementare un CMS per un sito web.Ho un'esperienza molto minima che usa JavaScript, jQuery, C #, ecc. Confuso principalmente da Java, SQL e C ++.La mia domanda è che ho l'istanza di ckeditor caricato sulla pagina.Sono in grado di caricare l'HTML che ho memorizzato nel mio database nella finestra CKeditor, ma non riesco a ottenere i valori che sono cambiato di nuovo da 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);
    }
}
.

Ho testato e so che sto scrivendo al database dal metodo SaveButton_Click.Una cosa che ho notato è che posso visualizzare un messaggio di avviso statico, come Alert ("Messaggio");Ma nessuna finestra di avviso si apre affatto con una delle linee del mio codice. Qualsiasi aiuto per ottenere questo set di questa configurazione in modo che io possa scrivere al mio database utilizzando la struttura della classe, o semplicemente ottenere getcontents () al lavoro sarebbe molto apprezzato.

È stato utile?

Soluzione

In Page_load È necessario controllare if (!Page.IsPostBack) prima di scrivere la proprietà Text del controllo CKEditor, altrimenti, su ogni post indietro (cioè pulsante clicca), il controllo avrà lo stesso valore dal database.-

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);
           }
        }
     }
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top