使用HTMLEditorExtender设置TextBox的值

时间:2012-11-07 18:42:58

标签: javascript textbox htmleditorextender

我有一个带有HTMLEditorExtender的asp.net文本框(ID =“HTMLTextBox_Comments”):

<asp:TextBox ID="HTMLTextBox_Comments" runat="server" Height="200px" Rows="5" 
        TextMode="MultiLine" Width="469px"></asp:TextBox>
<ajaxToolkit:HtmlEditorExtender ID="HTMLTextBox_Comments_HtmlEditorExtender" 
        runat="server" Enabled="True" TargetControlID="HTMLTextBox_Comments">
</ajaxToolkit:HtmlEditorExtender>

我正在使用javascript设置文本框的值:

var MyControl = document.getElementById("MainContent_HTMLTextBox_Comments"); //this gets the control just fine
MyControl.value = "Here's some text";

如果我有一个警告显示该值,则显示“这是一些文本”,但该文本不会显示在TextBox本身中。

我也尝试过设置MyControl.innerHTMLMyControl.text,但这些似乎都不起作用。

如何在TextBox中显示TextBox的值?

提前致谢!

修改 我发现this post似乎表明.value是我应该用来设置这个TextBox的文本,但它只是没有出现。我错过了什么?

2 个答案:

答案 0 :(得分:0)

WebForms为您的服务器端元素添加一个疯狂的ID。尝试document.querySelector()

http://jsfiddle.net/ZhGX7/

<input type="text" id="abc_123_MyDiv" value="firstValue" />

<script>

var input = document.querySelector("input[id*=MyDiv]");

input.value="Updated Value"
</script>

答案 1 :(得分:0)

我找到了解决方案here

诀窍是在 HTMLEditorExtender 中设置ExtenderContentEditable div的innerHTML,而不是直接尝试设置文本框值。值得注意的是,设置innerHTML也会设置值,因此您可以阅读TextBox.value并获取您输入的内容。

感谢您的帮助。

相关问题