Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Ajax Editors
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>
</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 16-Aug-12 5:19am
Edited 16-Aug-12 5:22am
v3
Comments
Ed Nutting at 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 at 4-Sep-13 5:55am
   
myField.focus() is not working
MikeVaros at 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
good
Please Sign up or sign in to vote.

Solution 1

[]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 410
1 Gihan Liyanage 338
2 ChauhanAjay 180
3 Vinay Mistry 160
4 Sergey Alexandrovich Kryukov 138
0 Sergey Alexandrovich Kryukov 9,011
1 OriginalGriff 7,941
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 23 Jul 2013
Copyright © CodeProject, 1999-2014
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