updatepanel和accordion出错

时间:2011-11-21 14:53:10

标签: jquery asp.net updatepanel

页面上有accordion control(using jQuery)。当我将页面内容插入updatepanel时,手风琴停止工作。如果我删除updatepanel,它就会开始工作。

为什么?
我该如何解决这个错误呢? 我是否必须重新实例化手风琴脚本/插件,如果是,那么如何?

  <script src="Scripts/ddaccordion.js" type="text/javascript"></script>
<script type="text/javascript">
   ddaccordion.init({
        headerclass: "silverheader", //Shared CSS class name of headers group
        contentclass: "submenu", //Shared CSS class name of contents group
        revealtype: "mouseover", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
        mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
        collapseprev: true, //Collapse previous content (so only one open at any time)? true/false
        defaultexpanded: [0], //index of content(s) open by default [index1, index2, etc] [] denotes no content
        onemustopen: true, //Specify whether at least one header should be open always (so never all headers closed)
        animatedefault: false, //Should contents open by default be animated into view?
        persiststate: true, //persist state of opened contents within browser session?
        toggleclass: ["", "selected"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
        togglehtml: ["", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
        animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
        oninit: function(headers, expandedindices) { //custom code to run when headers have initalized
            //do nothing
        },
        onopenclose: function(header, index, state, isuseractivated) { //custom code to run whenever a header is opened or closed
            //do nothing
        }
    })
</script>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
      <div class="applemenu">
        <div class="silverheader"><a href="http://www.dynamicdrive.com">Dynamic Drive</a></div>
        <div class="submenu">
            Some random content here<br />
       </div>
       <div class="silverheader"><a href="http://www.dynamicdrive.com/style/" >CSS Examples</a></div>
       <div class="submenu">
            Some random content here<br />
       </div>
       <div class="silverheader"><a href="http://www.javascriptkit.com">JavaScript Kit</a></div>
       <div class="submenu">
            Some random content here<br />
       </div>
       <div class="silverheader"><a href="http://www.cssdrive.com">CSS Drive</a></div>
       <div class="submenu">
           Some random content here<br />
          <img src="http://i27.tinypic.com/sy7295.gif" />
       </div>
       <div class="silverheader"><a href="http://www.codingforums.com">Coding Forums</a></div>
       <div class="submenu">
           Some random content here<br />
       </div>       
     </div>    
   </ContentTemplate>
</asp:UpdatePanel>

4 个答案:

答案 0 :(得分:3)

使用pageLoad,在所有部分回发中调用它:

$(document).ready() and pageLoad() are not the same!

 <script type="text/javascript">
    function pageLoad()
    {
       ddaccordion.init({
            headerclass: "silverheader", //Shared CSS class name of headers group
            contentclass: "submenu", //Shared CSS class name of contents group
            revealtype: "mouseover", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
            mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
            collapseprev: true, //Collapse previous content (so only one open at any time)? true/false
            defaultexpanded: [0], //index of content(s) open by default [index1, index2, etc] [] denotes no content
            onemustopen: true, //Specify whether at least one header should be open always (so never all headers closed)
            animatedefault: false, //Should contents open by default be animated into view?
            persiststate: true, //persist state of opened contents within browser session?
            toggleclass: ["", "selected"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
            togglehtml: ["", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
            animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
            oninit: function(headers, expandedindices) { //custom code to run when headers have initalized
                //do nothing
            },
            onopenclose: function(header, index, state, isuseractivated) { //custom code to run whenever a header is opened or closed
                //do nothing
            }
        })
    }
    </script>

答案 1 :(得分:1)

如果所有内容都在更新面板中,而不是将脚本放在更新面板中,它将开始工作。

答案 2 :(得分:0)

由于您的手风琴的整个内容/定义都在您的更新面板内,因此您每次更新面板更新时都必须重新启动它,因为您很可能获得手风琴的“新”副本并且任何事件都被绑定它消失了。

我们也遇到过这个问题(使用不同的网络控件),我们的解决方案是将最小化的内容放在更新面板中,比如隐藏按钮,然后对手风琴进行编码以“按下”这些按钮以便进行更新小组在没有输入手风琴的情况下完成了它的工作。

答案 3 :(得分:0)

我知道这是一篇旧帖子,但仍未解决。

所以,这里有两个问题:

第一个,正如@rickschott所指出的,你需要在每个部分回发后(不仅是第一次)初始化插件,因为插件在UpdatePanel内,所以它将停止工作UpdatePanel更新后。

当你解决这个问题时,会遇到第二个问题:似乎页面一直都在刷新,而且从不加载。这个问题可以视为ddaccordion jquery插件的错误。似乎它不是为了重置而设计的(或者在不从头开始加载页面的情况下销毁和重新创建)。我做了一个解决方法,但你可能需要测试它:

  • 打开ddaccordion.js并在其中找到init:function(config)
  • 删除这四行:

&#13;
&#13;
document.write('<style type="text/css">\n')
document.write('.'+config.contentclass+'{display: none}\n') //generate CSS to hide contents
document.write('a.hiddenajaxlink{display: none}\n') //CSS class to hide ajax link
document.write('<\/style>')
&#13;
&#13;
&#13;

  • 现在它将按预期工作。请记住将这两个css类添加到您的css文件中:

&#13;
&#13;
.submenu { display:none }
a.hiddenajaxlink { display:none }
&#13;
&#13;
&#13;

重要:建议不要修改第三方插件,因为每次更新插件时,都可能会丢失更改。但似乎ddaccordion不再由其作者维护,所以也许这可能是一个很好的解决方案,所以我希望它可以帮助其他人。