[英]How to keep the state of HTML div after postback
I use CKEditor in my webform application like this: 我在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>
I want to get editor1.InnerText
after postback, and I can't because it's stateless HTML div, and at the same time I can't use server control equivalent because I already handled the required events on this control. 我想在回editor1.InnerText
后获得editor1.InnerText
,但不能,因为它是无状态的HTML div,同时我不能使用等效的服务器控件,因为我已经处理了该控件上的必需事件。 How to keep the data after postback? 回发后如何保存数据?
Use a hidden Text Field as follows: 使用隐藏的文本字段,如下所示:
<asp:TextBox ID="tbTransfer" runat="server" TextMode="Multiline"></asp:TextBox>
In Javascript: 用Javascript:
//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");
}
In Code Behind: 在后面的代码中:
//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;
Encryption and Decryption of the HTML Code are necessary since Web Browsers will recognize the characters '<','>','&' as HTML tags and will not let you post back with plain HTML Text inside the Text box. HTML代码的加密和解密是必需的,因为Web浏览器会将字符“ <”,“>”,“&”识别为HTML标记,并且不允许您在“文本”框中使用纯HTML文本回发。
And do not forget to change 'MainContent_tbTransfer' if your text box has a different id in the browser 如果您的文本框在浏览器中具有不同的ID,请不要忘记更改“ MainContent_tbTransfer”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.