为什么我的控件会在回发时消失?

时间:2016-03-03 05:56:55

标签: c# jquery asp.net c#-4.0 tinymce

我在更新面板中使用了TinyMCE,但在每次部分回发后它都会消失,为什么?它应该适用于任何一种情况,即使官方网站也没有任何消息。

我用过这样的话:

 <script type="text/javascript">
        tinymce.init({
            selector: '#<%=txtBoxBody.ClientID%>',
            height: 200,
            theme: 'modern',
            plugins: [
              'advlist autolink lists pagebreak',
              //'searchreplace wordcount visualblocks visualchars fullscreen',
              'insertdatetime save contextmenu directionality',
              ' paste textcolor textpattern print'
            ],
            toolbar1: 'insertfile undo redo | fontsizeselect | forecolor emoticons | fontselect emoticons | styleselect | bold italic | alignleft aligncenter alignright alignjustify',
            //toolbar2: 'print preview media | forecolor backcolor emoticons',
            //image_advtab: true,
            fontsize_formats: '8pt 9pt 10pt 11pt 12pt 14pt 18pt 20pt 22pt 24pt 26pt 28pt 36pt 48pt 72pt',
            //templates: [
            //  { title: 'Test template 1', content: 'Test 1' },
            //  { title: 'Test template 2', content: 'Test 2' }
            //],
            content_css: [
              '//fast.fonts.net/cssapi/e6dc9b99-64fe-4292-ad98-6974f93cd2a2.css',
              '//www.tinymce.com/css/codepen.min.css'
            ]
        });


    </script>

on this txtbox 
<asp:TextBox runat="server" ID="txtBoxBody" Rows="20" Columns="25" TextMode="MultiLine" CssClass="form-control"></asp:TextBox>

它出现在首页加载但不是回发后 UPDATE:

它在pageload事件上尝试了这个

 //ScriptManager.RegisterStartupScript(this, this.GetType(), txtBoxBody.UniqueID + "Add", "tinyMCE.execCommand('mceAddEditor', true,'" + txtBoxBody.ClientID + "');", true);
        //ScriptManager.RegisterOnSubmitStatement(this, this.GetType(), txtBoxBody.UniqueID + "Remove", "tinyMCE.execCommand('mceRemoveEditor', true,'" + txtBoxBody.ClientID + "');");

它使它可见,但使形式充满麻烦,即完整的回发和不同的问题。

更新:我使用了javascript的pageLoad并将此代码放入其中,但插件已消失。

更新:然后我尝试了Piyush的建议

<script type="text/javascript">
    function SetEditor() {
        tinymce.init({
            selector: '#<%=txtBoxBody.ClientID%>',
            height: 200,
            theme: 'modern',
            plugins: [
              'advlist autolink lists pagebreak',
              //'searchreplace wordcount visualblocks visualchars fullscreen',
              'insertdatetime save contextmenu directionality',
              ' paste textcolor textpattern print'
            ],
            toolbar1: 'insertfile undo redo | fontsizeselect | forecolor emoticons | fontselect emoticons | styleselect | bold italic | alignleft aligncenter alignright alignjustify',
            //toolbar2: 'print preview media | forecolor backcolor emoticons',
            //image_advtab: true,
            fontsize_formats: '8pt 9pt 10pt 11pt 12pt 14pt 18pt 20pt 22pt 24pt 26pt 28pt 36pt 48pt 72pt',
            //templates: [
            //  { title: 'Test template 1', content: 'Test 1' },
            //  { title: 'Test template 2', content: 'Test 2' }
            //],
            content_css: [
              '//fast.fonts.net/cssapi/e6dc9b99-64fe-4292-ad98-6974f93cd2a2.css',
              '//www.tinymce.com/css/codepen.min.css'
            ]
        });


    }
</script>
<script type="text/javascript">
    // below will execute first time when page load
    SetEditor();

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

    function EndRequestHandler(sender, args) {
        // this will execute on partial postback
        SetEditor();
    }
</script>

但同样的问题

3 个答案:

答案 0 :(得分:0)

要解决此问题,请按以下步骤操作

  1. 创建setEditor javascript函数,如下所示,

    <script type="text/javascript">
    function SetEditor() {
        tinymce.init({
        ...
        });
    }
    </script>
    
  2. 在body标记结束之前,添加以下脚本,

    <script type="text/javascript">
        // below will execute first time when page load
        SetEditor();
    
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    
        function EndRequestHandler(sender, args) {
            // this will execute on partial postback
            SetEditor();          
        }
    </script>
    

答案 1 :(得分:0)

将init封装在一个函数中 然后确保使用scriptmanager注册init函数 比如说 ScriptManager.RegisterStartupScript(this.Page,          this.Page.GetType(),mce.ClientID,“pageLoad();”,true);

答案 2 :(得分:0)

@Piyush提供了很多帮助,稍作修改就解决了这个问题。

  ScriptManager.RegisterStartupScript(this, this.GetType(), txtBoxBody.UniqueID + "Add", "tinyMCE.execCommand('mceAddEditor', true,'" + txtBoxBody.ClientID + "');", true);
  ScriptManager.RegisterOnSubmitStatement(this, this.GetType(), txtBoxBody.UniqueID + "Remove", "tinyMCE.execCommand('mceRemoveEditor', true,'" + txtBoxBody.ClientID + "');");