How do you get/set content via javascript with the AJAX Control Toolkit HTML Editor?

不羁的心 提交于 2019-12-05 22:13:42

The Html Editor is one of the unique Ajax Control Toolkit controls, becuase it does not inherit AjaxControlToolkit.ExtenderControlBase (server side) nor inherit AjaxControlToolkit.BehaviorBase (client side).

So you can't use $find javascript method to get access to the behavior instance on the client, It inherits AjaxControlToolkit.ScriptControlBase (server side) and Sys.UI.Control (client side).

To get access to control instance on the client, you use the control property on the DOM element it self as follows:

<script type="text/javascript">
//considering the editor is loaded.
var editorControl = $get("<%=editor.ClientID%>").control;

//1. For setting content:
editorContorl.set_content("Sample Content");

//2. For getting content:
var content = editorContorl.get_content();    
</script>
thomas Chai
$(function(){
    $find("editor").set_content("jQuery set content");
    alert($find("editor").get_content());
});

Above answers didn't work for me. So I had to dig through html and got following working solution. its working for years now and survived many framework/OS/browsers update/upgrade.

<script type="text/javascript">
    var varcont = '<%=txtInstructions.ClientID %>' + '_ctl02_ctl00';
    //Get Content
    var content = document.getElementById(varcont).contentWindow.document.body.innerHTML; 

    //Set Content
    document.getElementById(varcont).contentWindow.document.body.innerHTML='<b>I rock</b>'
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!