【问题标题】:Google Plus in an ASP.NET User ControlASP.NET 用户控件中的 Google Plus
【发布时间】:2012-03-14 15:11:27
【问题描述】:

我有一个 ASP.NET 4.0 主页,其中显示了大约 20 个产品列表。每个产品列表都会指向一个 URL,格式为:/DisplayProduct.aspx?ProdID=X

我想在每个列表的前面显示每个产品列表收到了多少 Google Plus 和 ReTweets。我怎样才能以最佳优化的方式做到这一点,而又不会过多地影响页面加载速度。必须使用哪些脚本?

这是带有代码的用户控件

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProductListing.ascx.cs" Inherits="Controls.ProductListing" %>

<asp:GridView ID="gvProducts" runat="server"  AllowPaging="True" AutoGenerateColumns="False"
   DataKeyNames="ID" DataSourceID="objProds" PageSize="10" ShowHeader="False"
   OnRowDataBound="gvProducts_RowDataBound" OnRowCommand="gvProducts_RowCommand"
    >
   <Columns>
      <asp:TemplateField HeaderText="Product List">
         <HeaderStyle  />
         <ItemTemplate>
            <div class="plist">
            <table cellpadding="0" cellspacing="0" style="width: 100%;">
    <tr><td>               
               <div class="productname">
               <asp:HyperLink runat="server" ID="hypName" Text='<%# Eval("Name") %>'
                  NavigateUrl='<%# "~/DisplayProduct.aspx?ID=" + Eval("ProdID") %>'/> 
               </div>               
            </td> </tr>
            </table>
             <div class="productdesc">
             <b>Description: </b>
            <asp:Literal runat="server" ID="lblDesc" Text='<%# Eval("Description") %>' />
            </div>
            </div>
         </ItemTemplate>         
      </asp:TemplateField>
   </Columns>   
</asp:GridView>

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-4.0


    【解决方案1】:

    你应该这样做

    <!-- Place this tag where you want the +1 button to render -->
    <g:plusone size="small" annotation="none" href="http://www.yourwebsite.com/DisplayProduct.aspx?ProdID=<%# Eval("Name") %>"></g:plusone>
    

    以上是您想要为每个产品重复的内容,每个产品具有指向该产品页面的不同 url。

    并且下面的脚本不应该重复。

    <!-- Place this render call where appropriate -->
    <script type="text/javascript">
      (function() {
        var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
        po.src = 'https://apis.google.com/js/plusone.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
      })();
    </script>
    

    希望对你有帮助。

    【讨论】:

    • 我应该在用户控件中添加这个脚本吗?它适合哪里?此外,产品网址是动态创建的。那么我可以在 >g:plusone 脚本中指定它
    • 您应该以与在链接中设置 NavigateUrl 相同的方式分配 href。脚本最好放在页面底部(这样浏览器会先显示内容,然后再处理脚本)
    • 我会将此脚本保留在页面底部,因为您可能在其他地方有 g+ 按钮。
    • 我试过了,但没用 - yourwebsite.com/DisplayProduct.aspx?ProdID=" + Eval("ProdID") >:plusone>
    • 查看更新后的帖子。我用你的 ProdID 替换了 X。如果您仍然有任何问题,请告诉我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 2014-12-13
    • 2012-08-31
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多