How to get contents of CKEDITOR when postingback in asp.net

那年仲夏 提交于 2019-12-10 16:34:58

问题


I used CKEDITOR in my ASP.NET project, the page contains asp:TextBox with TextMode="Multiline" and a linkbutton. When I press linkbutton I can't get the TextBox value in postback.. No errors occured... How to get content in server side? I was thinking of use jQuery to track changes on content of CK and copy it to the hidden textarea..but didn't seem to be right.

I'm using javascript version of CK v4.2, not .net library version.

Addition:
In the sample downloaded with CKEditor.zip file you can see that getting text in server side is easy using $_POST.
Why in ASP.NET isn't?

Update:
This issue occurs when using RadScriptManager, RadAjaxManager and RadAjaxPanel (from telerik).


回答1:


On the ASPX Page set the CKEditor control as:

<CKEditor:CKEditorControl ID="CKEditor1" runat="server"/>

On the code behind page:

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        string text1 = CKEditor1.Text;
        string text2 = CKEditor1.Value;
        ...
    }
}

Remember to add/include proper references to CKEditor binaries




回答2:


I faced the same problem. On a simple page I tested a LinkButton and a Button. The problem is that a LinkButton does not submit the form directly. It renders as a link and calls __doPostBack event so this is a problem for CKEditor. I tried to set the textarea like Nirmal suggested but that didn't work too. Here is how I solved it.

Header:

<script type="text/javascript">
    $(document).ready(function () { $("#tBody").ckeditor(); });

    function setValue() {
        $("#hfBody").val(CKEDITOR.instances.tBody.getData());
    }
</script>

Inside the form:

<asp:TextBox ID="tBody" runat="server" TextMode="MultiLine"/>
<asp:HiddenField ID="hfBody" runat="server" />
<asp:LinkButton ID="btnSend" Text="Link" runat="server" OnClick="X_Click"
OnClientClick="setValue()"/>

C#

protected void X_Click(object sender, EventArgs e)
{
    divResult.InnerHtml = hfBody.Value;
    tBody.Text = hfBody.Value;
}



回答3:


something like this might help

string newText = ((CKedit.CKeditor)(e.Item.FindControl("txtBox"))).Value;

else use updatepanel to retain its value on postback




回答4:


One of my co-workers solved it by setting setup property when init tinymce like this:

setup: function (ed) {
    ed.on('change', function (e) {
        ed.save();
    });
}

This will save content to editor when change event called.




回答5:


You have will get the data try below code :

var editorText = CKEDITOR.instances.txtinstruction.getData(); _msemailsetup.BodyMessage = editorText ;




回答6:


before firing postback event call this function for set value

function setValue() 
{
     $("#txtinstruction").val(CKEDITOR.instances.txtinstruction.getData());
}

txtinstruction is id of textbox or textarea



来源:https://stackoverflow.com/questions/18379548/how-to-get-contents-of-ckeditor-when-postingback-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!