Click here to Skip to main content
13,056,229 members (56,658 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have been having a lot of issues as I try to insert ComboBox values into the Ajax Html editor. I have seen a few questions on here related, and I've tried several of the scripts that are listed as accepted answers, but I still cannot get the values to insert at all. I am using Visual Studio 2010, and it is an ASP.Net WebApp. I cannot for the life of me understand why it will not insert the values, when people obviously have no issues with it. I am trying to insert values from the ComboBox to act as placeholders for Database Values that will be inserted at a later time. I'll post my code here, and I would really appreciate any help or advice anyone can offer.

I have tried several different scripts in an attempt to get this working, but none have yielded any results. The furthest I was able to get was one of the scripts appeared to refresh the page on the SelectedIndex Changed event, and wipe any content that existed in the editor. That wasn't at all helpful, and I could not understand why it even did that.

<asp:ComboBox ID="ComboBox1" runat="server">
    <asp:ListItem Value="%meternumberFieldHolder%">Meter Number</asp:ListItem>
    <asp:ListItem Value="%accountnumberFieldHolder%">Account Number</asp:ListItem>
        <br />
        <cc1:Editor ID="Editor1" runat="server" Height="500px" />
        &nbsp;<script type="text/javascript">
            $(document).ready(function () {
                $('#<%:ComboBox1.ClientID%>').change(function () {
                    var cbtext = $('#<%:ComboBox1.ClientID%> option:selected').text();
                    var cbtext = ' [' + ddltext + '] '
                    InsertAtCursor(Editor1, cbtext); //Function for Insertion
            function InsertAtCursor(myField, myValue) {
                if (document.selection) {
                    sel = document.selection.createRange();
                    sel.text = myValue;
                else if (myField.selectionStart == 0 || myField.selectionStart == '0') {
                    var startPos = myField.selectionStart;
                    var endPos = myField.selectionEnd;
                    myField.value = myField.value.substring(0, startPos) + myValue +
                        myField.value.substring(endPos, myField.value.length);
                else {
                    myField.value += myValue;

This is adapted from an answer I found here[^]
And people there seem to have no issues with this, but I cannot get it to work.
Posted 16-Aug-12 5:19am
Updated 16-Aug-12 5:22am
Ed Nutting 18-Aug-12 21:15pm

Well I can spot two issues with this:
1) cbtext should not be declared twice (the original code got.this wrong too)
2) You have changed ddltext to cbtext but the second cbtext line you forgot to change it to `[` + cbtext + `]` - this may well throw an error preventing your code from working.

Finally, don`t forget two include jQuery first in your page head section.

Hope this helps,

(Oh and where is the variable Editor1 declared/created?)
Member 10021302 4-Sep-13 5:55am
myField.focus() is not working
MikeVaros 20-Aug-12 8:39am
I didn't notice that ddltext in this example I posted, but I've tried several combinations of those variables. I have the editor declared as

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit.HTMLEditor" tagprefix="cc1" %> And then later on as <cc1:Editor ID="Editor1" runat="server" Height="500px" /> as shown above.

The only way I can seem to get this to work is to sack the Jquery completely and try to append it to the end of the Html Editor's content on the SelectedIndexChanged event of the ComboBox. But this doesn't meet my requirement. I have been pulling my hair out for days trying to figure this out, and I have gotten nowhere. Any help you can offer would be greatly appreciated.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170728.2 | Last Updated 23 Jul 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100