如何让用户控制加载一次?

时间:2015-09-15 19:11:19

标签: c# asp.net

我有一个名为“SocialShareElements”的用户控件,其目的是将页面内容/图像共享到FaceBook中。 我在索引页面中使用了这个用户控件。

我在Index.aspx中将此用户控件调用为“IF”状态。 伪代码如下:

<% 
   if (Request.IsSecureConnection)
  {
%>
   <div class="deal-detail-head tzsg-margin-bottom-med">
     <MyControl:SocialShareElements ID="SocialShareElements1" runat="server"/> 
   </div>
   <div class="clear-both"></div>
<%
  }
  else
 {
 %>
   <div class="deal-test bottom-small">
     <MyControl:SocialShareElements ID="SocialShareElements" runat="server"/> 
   </div>
<% 
 } 
%>

当我在https://developers.facebook.com/tools/debug/og/object/中测试FBShare功能时,它显示失败,因为我加载了两次“SocialShareElements”,这使得“SocialShareElements”的元数据在Index.aspx中加倍。

我的问题是如何调整在Index.aspx中使用“SocialShareElements”的逻辑,使其只加载一次。

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以通过代码隐藏实现此目的。

修改您的aspx页面上的div,以便从服务器端获取:

<div id="socialShare" runat="server">

</div>

在您的代码隐藏中:

 protected void Page_Load(object sender, EventArgs e)
 {
    if(Request.IsSecureConnection)
    {
        SocialShareElements shareElement = LoadControl("SocialShareElements.ascx") as SocialShareElements;

        socialDiv.Controls.Add(shareElement);
    }
 }