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

徘徊边缘 提交于 2019-12-22 09:49:45

问题


I'm using the AJAX Control toolkit HTML editor and have what I hope is a simple question. As the question title says - how do you get/set the content of the HTML editor via javascript?

I have no problems accessing the server side content property - but how to do it client side?

Any help gratefully received !


回答1:


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>



回答2:


$(function(){
    $find("editor").set_content("jQuery set content");
    alert($find("editor").get_content());
});



回答3:


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>


来源:https://stackoverflow.com/questions/3342557/how-do-you-get-set-content-via-javascript-with-the-ajax-control-toolkit-html-edi

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