【问题标题】:Facebook Like & Google +1 button disappears on partial postbackFacebook Like & Google +1 按钮在部分回发时消失
【发布时间】:2011-10-09 11:33:17
【问题描述】:

我在 webpart 中放置了 facebook like 和 google + 按钮,而 webpart 又位于更新面板中。问题是一旦通过更新面板进行异步回发,fb 和 g+ 按钮就会消失。

我还有一个 twitter 按钮,我之前遇到过类似的问题。在我将 twitter javascript 放在更新面板之外之后,它现在可以工作了:

<asp:updatepanel ID="UpdatePanel1" runat="server">
     <asp:WebPartManager ID="WebPartManager1" runat="server">
     </asp:WebPartManager>
     <script type="text/javascript">              
            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);                
     </script>
     <asp:webpartzone ID="WebPartZone1" runat="server" PartChromeType="None" >
       <ZoneTemplate>
       .
       .
       .
        <a href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet Profile</a><br />
        <div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=181772605219534&amp;xfbml=1"></script><fb:like href="" send="false" layout="button_count" width="50" show_faces="false" font="trebuchet ms"></fb:like>
        <br/>
        <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
        <g:plusone size="medium" count="true"></g:plusone>
         .
         .
         </ZoneTemplate>
     </asp:webpartzone>
     <asp:webpartzone ID="WebPartZone2" runat="server" Width="100%" 
                            PartChromeType="None">
         .
         .
         .
     </asp:webpartzone>
</asp:updatepanel>

<script type="text/javascript">
    function pageLoad() {
        $.getScript("http://platform.twitter.com/widgets.js");
    }
</script>

这在 twitter 推文按钮的部分回发后重新加载了损坏的 javascript,它工作正常。但是当我替换 javascripts 时,facebook 和 google plus 按钮不会发生同样的情况。我正在使用 ASP.NET 4 和 c#。我该如何解决这个问题??

【问题讨论】:

标签: javascript asp.net facebook facebook-like google-plus-one


【解决方案1】:

UpdatePanel 通过设置innerHTML 属性来更新其内容。这意味着在 UpdatePanel 异步回发之后,UpdatePanel 元素中的任何 DOM 状态都会丢失。 fb、g+ 和 twitter 按钮都与 UpdatePanel div 的 DOM 状态一起工作,服务器在将 innerHTML delta 发送到客户端时将其删除。

在这里查看我的答案:RegisterClientScriptCode not working after a partial post back

您需要为 webpart 禁用异步回发,或者将脚本放在其他地方。

更新问题

你试过了吗?删除 &lt;script&gt; 元素并使用 jquery 调用它们:

<asp:updatepanel............>
     <asp:WebPartManager ID="WebPartManager1" runat="server" />
     <asp:webpartzone...........>
       .
       .
       .
        <a href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet Profile</a><br />
        <div id="fb-root"></div><fb:like href="" send="false" layout="button_count" width="50" show_faces="false" font="trebuchet ms"></fb:like><br/>
        <g:plusone size="medium" count="true"></g:plusone>
     </asp:webpartzone>
</asp:updatepanel>

<script type="text/javascript">
    function pageLoad() {
        $.getScript("http://platform.twitter.com/widgets.js");
        $.getScript("http://connect.facebook.net/en_US/all.js#appId=181772605219534&amp;xfbml=1");
        $.getScript("https://apis.google.com/js/plusone.js");
    }
</script>

如果这不起作用,为什么不将按钮分离到一个静态面板中呢?这些按钮对 UpdatePanel 没有用处。像这样的:

<!-- webpartmanager does NOT need to enclose your webparts. 
     Just declare it. -->
<asp:webpartmanager ... /> 

<!-- asp:updatepanel contains the async webpart. -->
<asp:updatepanel ...> 
    <asp:WebPartZone id="WebPartZone1" runat="server">
      <ZoneTemplate>
        ...
      </ZoneTemplate>
    </asp:WebPartZone>
</asp:updatepanel>

<!-- another webpart holds the buttons, NOT contained by updatepanel. -->
<asp:WebPartZone id="WebPartZone2" runat="server">
  <ZoneTemplate>
    <a href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet Profile</a><br />
    <div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=181772605219534&amp;xfbml=1"></script><fb:like href="" send="false" layout="button_count" width="50" show_faces="false" font="trebuchet ms"></fb:like><br/>
    <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
    <g:plusone size="medium" count="true"></g:plusone>
  </ZoneTemplate>
</asp:WebPartZone>

【讨论】:

  • 如果我将 javascript 与我的情况分开,fb 按钮就会消失
  • 我的意思是您需要将按钮完全移动到其他位置,而不是在 UpdatePanel 中。
  • 但我必须在更新面板中使用它们。 Tweet 按钮现在工作得很好,但也面临同样的问题。一定有办法让 fb 和 google 按钮也能正常工作。
  • 是的,我也这样做了。当我这样做时,fb 和 +1 按钮消失,而 twitter 按钮仍然存在。我需要 webpart 的异步回发,这就是为什么我将其保留在更新面板中的原因。当在 gui 中我需要它们在 webpart 中时,如何将它们分成静态面板??
  • 使用两个不同的 webpart——一个在更新面板中,一个包含按钮。顺便说一句,没有必要将所有内容都包含在 &lt;asp:webpartmanager&gt; 中,它只需要在页面的某个位置即可。
【解决方案2】:

这似乎是非常不寻常的解决方案,但它适用于 chrome 和 firefox。

当我将 facebook 的 javascript src 链接保留在更新面板内并排除更新面板(jquery 调用)时,它起作用了。对于 Twitter 和 Google + 按钮,它只需要在更新面板之外由 jquery 调用。当我使用以下组合时它起作用了:

<asp:updatepanel............>
 <asp:WebPartManager ID="WebPartManager1" runat="server">
 </asp:WebPartManager>
 <asp:webpartzone...........>
   .
   .
   .
    <a href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet Profile</a><br />
    <div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=181772605219534&amp;xfbml=1"></script><fb:like href="" send="false" layout="button_count" width="50" show_faces="false" font="trebuchet ms"></fb:like>
    <br/>
    <g:plusone size="medium" count="true"></g:plusone>
     .
     .
 </asp:webpartzone>
 <asp:webpartzone ..........>
     .
     .
     .
 </asp:webpartzone>
</asp:updatepanel>
<script type="text/javascript">
  function pageLoad() {
    $.getScript("http://platform.twitter.com/widgets.js");          
    $.getScript("http://connect.facebook.net/en_US/all.js#appId=181772605219534&amp;xfbml=1");
    $.getScript("https://apis.google.com/js/plusone.js");
            }
</script>

注意: google +1 按钮在 Internet Explorer 中仍然无法使用,但所有 3 个按钮在 Chrome 和 Firefox 中都可以正常使用。

【讨论】:

  • 是的,这和我的回答几乎一样。 pageLoad() 在每次异步回发时重新运行。您可以使用一些有创意的 HTML 和 CSS 将按钮从更新面板中移除。
  • @Justin 然后将您的答案编辑为相同。请提供如何使用创意html部分的解决方案。这不是最终的解决方案,因为 g+ 按钮仍然不适用于 IE....
  • 我需要查看您的其他元素(为简洁起见,您剪掉了“...”)。请记住,UpdatePanel 只是一个div,所以取出按钮,如果需要将异步元素分解为多个 UpdatePanel,就可以实现您想要的布局...
【解决方案3】:

我总是将它们排除在部分回发之外。所有广告、+1 和点赞按钮都会从更新面板中排除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多