English 中文(简体)
updatepanel和手风琴错误
原标题:error with updatepanel and accordion

我在页面上有一个手风琴控件(使用jQuery)。当我将页面的内容插入更新面板时,手风琴停止工作。如果我删除更新面板,它就会开始工作。

Why?
How can i fix this error?
Do i have to re-instantiate the accordion script/plugin, If yes then how?

  <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>
问题回答

使用<code>pageLoad</code>,它在所有部分回发时都会被调用:

$(document).ready()和pageLoad()不一样

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

如果所有内容都在更新面板中,那么只需将脚本放入更新面板中即可开始工作。

由于手风琴的整个内容/定义都在更新面板中,因此每次更新面板更新时,您都必须重新初始化它,因为您必须获得手风琴的新副本,并且与之相关的任何事件都已消失。

我们也遇到过这个问题(使用不同的控件),我们的解决方案是将我们必须做的最起码的事情放在更新面板内,比如隐藏按钮,然后对手风琴进行编码以按下这些按钮,这样更新面板就可以在不冲洗手风琴的情况下完成任务。

我知道这是一个老帖子,但它仍然没有解决。

所以,这里有两个问题:

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

当您修复该问题时,您会遇到第二个问题:页面似乎一直在刷新,但从未加载。这个问题可以被认为是ddaccordion jquery插件的一个bug。似乎它不是为了重置(或在不从头加载页面的情况下销毁和重新创建)而设计的。我做了一个变通方法,但您可能必须进行测试:

  • Open ddaccordion.js and find init:function(config) in it.
  • Remove this four lines:

document.write( <style type="text/css">
 )
document.write( . +config.contentclass+ {display: none}
 ) //generate CSS to hide contents
document.write( a.hiddenajaxlink{display: none}
 ) //CSS class to hide ajax link
document.write( </style> )
  • Now it will work as expected. Remember to add those 2 css classes to your css file:

.submenu { display:none }
a.hiddenajaxlink { display:none }

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签