Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
4.00/5 (1 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.NET
<asp:ComboBox ID="ComboBox1" runat="server">
    <asp:ListItem Value="%meternumberFieldHolder%">Meter Number</asp:ListItem>
    <asp:ListItem Value="%accountnumberFieldHolder%">Account Number</asp:ListItem>
</asp:ComboBox>

        <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) {
                    myField.focus();
                    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;
                }
            }
        </script>


This is adapted from an answer I found here http://stackoverflow.com/questions/10509707/insert-text-at-cursor-position-in-ajax-html-editor-using-client-script/10554972#10554972[^]
And people there seem to have no issues with this, but I cannot get it to work.
Posted
Updated 16-Aug-12 5:22am
v3
Comments
Ed Nutting 18-Aug-12 21:15pm    
Hi,

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,
Ed

(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

[]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900