How to make TinyMCE work inside an UpdatePanel?

前端 未结 12 904
别跟我提以往
别跟我提以往 2021-02-07 13:28

I\'m trying to do something that many people seem to have been able to do but which I am unable to implement any solution. The TinyMCE control works pretty well in an asp.net fo

相关标签:
12条回答
  • 2021-02-07 13:53

    Updating the answer to this question for those using .NET framework 4, I was successful in attaching TinyMCE to a TextBox inside an update panel by inserting the following:

    In markup within the <head></head> region:

    <script src="scripts/tinymce/tinymce.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    
        tinyMCE.init({
            selector: ".tinymcetextarea",
            mode: "textareas",
    
            plugins: [
                 "advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",
                 "searchreplace visualblocks visualchars code fullscreen autoresize insertdatetime media nonbreaking",
                 "save table contextmenu directionality emoticons template paste textcolor",
                 "autosave codesample colorpicker image imagetools importcss layer"
            ],
    
            toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | print preview media | forecolor backcolor emoticons",
            style_formats: [
                 { title: 'Bold text', inline: 'b' },
                 { title: 'Red text', inline: 'span', styles: { color: '#ff0000' } },
                 { title: 'Red header', block: 'h1', styles: { color: '#ff0000' } },
                 { title: 'Example 1', inline: 'span', classes: 'example1' },
                 { title: 'Example 2', inline: 'span', classes: 'example2' },
                 { title: 'Table styles' },
                 { title: 'Table row 1', selector: 'tr', classes: 'tablerow1' }
            ]
        });
    
    </script>
    

    In the markup within the <body></body> region:

    <asp:TextBox ID="tbContentHtml" CssClass="tinymcetextarea" Wrap="true" runat="server" Width="90%" TextMode="MultiLine" />
    

    And finally in codebehind in the Page_Load event:

    ScriptManager.RegisterStartupScript(this, this.GetType(), tbContentHtml.UniqueID + "Add", "tinyMCE.execCommand('mceAddEditor', true,'" + tbContentHtml.ClientID + "');", true);
    ScriptManager.RegisterOnSubmitStatement(this, this.GetType(), tbContentHtml.UniqueID + "Remove", "tinyMCE.execCommand('mceRemoveEditor', true,'" + tbContentHtml.ClientID + "');");
    
    0 讨论(0)
  • 2021-02-07 13:54

    i solved this problem as call tiny mce after the response generation of the ajax call

    function edittemp(name) {
    
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
    alert ("Your browser does not support XMLHTTP!");
    return;
    }
    
    
    var url="edit_temp.php";
    url=url+"?id="+name;
    
    
    
    
    
    xmlhttp.onreadystatechange=stateChanged3;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null); 
    
    
    }
    function stateChanged3()
    {
    if (xmlhttp.readyState==4)
    {
    spl_txt=xmlhttp.responseText.split("~~~");
    
    
    document.getElementById("edit_message").innerHTML=spl_txt[0];   
    tinyMCE.init({
    theme : "advanced",
    mode: "exact",
    elements : "elm1",
    theme_advanced_toolbar_location : "top",
    theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,"
    + "justifyleft,justifycenter,justifyright,justifyfull,formatselect,"
    + "bullist,numlist,outdent,indent",
    theme_advanced_buttons2 : "link,unlink,anchor,image,separator,"
    +"undo,redo,cleanup,code,separator,sub,sup,charmap",
    theme_advanced_buttons3 : "",
    height:"350px",
    width:"600px"
    });
    }
    }
    

    and the page caaled by ajax call is

    <?php
    $name=$_GET['id'];
    include 'connection.php';
    $result=mysql_query("SELECT * FROM `templete` WHERE temp_name='$name' and status=1");
    
    $row = mysql_fetch_array($result);
    $Content=$row['body'];
    ?>
    <html>
    <head>
    <title>editing using tiny_mce</title>
    <script language="..." src="tinymce/jscripts/tiny_mce  /tiny_mce.js"></script>
    </head>
    <body>
    <h2>change the template here</h2>
    <form method="post" action="save_temp.php?name=<?php echo $name;?>">
    <textarea id="elm1" name="elm1" rows="15" cols="80"><?php echo $Content;?></textarea>
    <br />
    <input type="submit" name="save" value="Submit" />
    <input type="reset" name="reset" value="Reset" />
    </form>
    </body>
    </html>
    

    may be helpful in such situation.

    0 讨论(0)
  • 2021-02-07 13:56

    Not sure if you've looked at these.

    http://joakimk.blogspot.com/2007/07/tinymce-inside-of-aspnet-ajax.html

    and

    http://codeodyssey.com/archive/2007/7/18/updatepanel-tinymce-demo-with-project-zip-file

    Here is a tinymce forum post on it

    http://tinymce.moxiecode.com/punbb/viewtopic.php?id=12682

    Good luck.

    0 讨论(0)
  • 2021-02-07 14:01

    I did the following:

    First I added the this Javascript to my page:

    <script type="text/javascript">
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
    
    function endRequestHandler(sender,args)
    {
        tinyMCE.idCounter=0;
        tinyMCE.execCommand('mceAddControl',false,'htmlContent');
    }
    
    function UpdateTextArea()
    { 
        tinyMCE.triggerSave(false,true);
    }
    </script>
    

    Because I'm creating an ASP.NET and using and ASP.NET Button in my page, I had to add the following to the Page Load:

    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Attributes.Add("onclick", "UpdateTextArea()");
    }
    
    0 讨论(0)
  • 2021-02-07 14:01

    TinyMCE (as well as other WYSIWYG editors, FCKEditor etc) suffers from postback validation issues. By default any ASP.Net page on postback has its contents checked, and any unencoded HTML throws the postback validation error.

    Now many people, including on those forums suggest disabling the postback validation, validaterequest="false" , but this makes you susceptible to scripting attacks, the best thing to do is bind a function to the async postback event that fires off just before async postback. This JavaScript function needs to HTML encode the TinyMCE data being posted back to the server, this will then pass the postback validation and you'll be OK.

    I believe TinyMCE and other editors correctly do this on postbacks but not async postbacks hence the issue, in fact if you look at TinyMCE's source you can probably find their function that does this and simply add the event binding.

    Hope this helps

    0 讨论(0)
  • 2021-02-07 14:03

    To execute the init everytime the UpdatePanel changes you need to register the script using ScriptManager:

    // control is your UpdatePanel
    ScriptManager.RegisterStartupScript(control, control.GetType(), control.UniqueID, "your_tinymce_initfunc();", true);
    

    NOTE: You cannot use exact mode on your init function, you can use either textareas or a class selector, or else it won't work properly.

    You also have to use

    ScriptManager.RegisterOnSubmitStatement(this, this.GetType(), "", "tinyMCE.triggerSave();");
    

    On a postback of a UpdatePanel the editor content isn't saved on the Textbox, because the default behavior is only for form.submit, so when you submit anything it will save the text before it posts.

    On the code behind to get the value you will just need to access TextBox.Text property.

    NOTE: If you are using the .NET GZipped you probably will have to drop it, I couldn't get it working, I had to remove this completely.

    0 讨论(0)
提交回复
热议问题