回发后如何保持HTML div的状态

时间:2019-07-10 10:16:43

标签: javascript c# asp.net ckeditor viewstate

我在Webform应用程序中像这样使用CKEditor

  <div class="editor" runat="server" EnableViewState="true">
       <div cols="10" id="editor1" name="editor1" data-sample-short contenteditable="true" runat="server" EnableViewState="true">
          TEST
       </div>
   </div>

我想在回发后得到editor1.InnerText,但不能,因为它是无状态的HTML div,同时我不能使用等效的服务器控件,因为我已经处理了该控件上的必需事件。 回发后如何保存数据?

1 个答案:

答案 0 :(得分:0)

使用隐藏的文本字段,如下所示:

<asp:TextBox ID="tbTransfer" runat="server" TextMode="Multiline"></asp:TextBox>

在Java语言中:

//Set Transfer Textbox display to none
document.getElementById('MainContent_tbTransfer').style.display = "none";

//Set Initial Source Code of CKEditor to Transfer TextBox's value
CKEDITOR.instances.editor1.setData(document.getElementById('MainContent_tbTransfer').value);

//Use this function for when you want to do the PostBack
function PostBack() {
    var temp = CKEDITOR.instances.editor1.getData();

    document.getElementById('MainContent_tbTransfer').value = temp.replace(/</g, "d123").replace(/>/g, "d321").replace(/&/g,"d111");
}

在后面的代码中:

//Getting the HTML source code of CKEditor from the Transfer Textbox and Decrypting it
string htmlToSave = tbTransfer.Text.Replace("d123", "<").Replace("d321", ">").Replace("d111", "&");

//Setting back the Transfer Textbox's text to the decrypted HTML source code
//for when the page reloads at the end of the function,
//CKEditor sets its data = Transfer Textbox's Text
tbTransfer.Text = htmlToSave;

HTML代码的加密和解密是必要的,因为Web浏览器会将字符“ <”,“>”,“&”识别为HTML标记,并且不允许您在文本框中使用纯HTML文本回发。 / p>

如果您的文本框在浏览器中具有不同的ID,请不要忘记更改“ MainContent_tbTransfer”

相关问题