将动态属性绑定到用户控件

时间:2012-06-12 23:42:01

标签: c# asp.net webforms

嗨伙伴stackoverflowers(如果这是一个字!),

我正在尝试使用动态属性的用户控件,但无法让它以我想要的方式工作。控件的目的是拥有通用的AddThis小部件,设计人员可以将其置于ASPX中,而无需开发人员在ASPX.CS中执行任何操作。以前,我只在ASPX页面的代码隐藏中设置了用户控件的动态属性,但现在让程序员和设计人员参与这样的简单任务似乎太麻烦了。

这是我的代码:

用户控制码(ASCX):

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddThisSocial.ascx.cs"
    Inherits="MyWebsite.Controls.AddThisSocial" %>
<!-- Begin AddThis Social Share -->
<div class="addthis_toolbox addthis_default_style" addthis:url="<%=Url%>" addthis:title="<%=Title %>"
    addthis:description="<%=Description%>">
    <%
        if (Facebook)
        {
    %>
    <a class="addthis_button_facebook" title="Send to Facebook" style="margin-top: 5px;">
    </a>
    <%
        }
    %>
    <%
        if (Twitter)
        {
    %>
    <a class="addthis_button_twitter" title="Send to Twitter" style="margin-top: 5px;">
    </a>
    <%
        }
    %>
    <%
        if (Google)
        {
    %>
    <a class="addthis_button_google" title="Send to Google" style="margin-top: 5px;">
    </a>
    <%
        }
    %>
    <%
        if (Pinterest)
        {
    %>
    <a class="addthis_button_pinterest_pinit" style="margin-top: 0px;">
    </a>
    <%
        }
    %>
    <%
        if (CompactButton)
        {
    %>
    <a class="addthis_button_compact" style="margin-top: 5px;"></a>
    <%
        }
    %>
</div>
<!-- End AddThis Social Share -->

用户控制代码(ASCX.CS):

namespace MyWebsite.Controls
{
    public partial class AddThisSocial : System.Web.UI.UserControl
    {

        #region Public Properties

        public string Url { get; set; }        
        public string Title { get; set; }        
        public string Description { get; set; }        
        public string ImageUrl { get; set; }

        [DefaultValue("False")]        
        public Boolean Facebook { get; set; }
        [DefaultValue("False")]        
        public Boolean Twitter { get; set; }
        [DefaultValue("False")]        
        public Boolean Google { get; set; }
        [DefaultValue("False")]        
        public Boolean Pinterest { get; set; }
        [DefaultValue("False")]        
        public Boolean CompactButton { get; set; }

        #endregion

        #region Page Events

        protected void Page_Load(object sender, EventArgs e)
        {

            // facebook doesn't like addthis attributes - add opengraph attributes seperately
            var page = HttpContext.Current.Handler as Page;
            if (page == null) return;

            if(!String.IsNullOrEmpty(Url))
            {
                var mUrl = new HtmlMeta();
                mUrl.Attributes.Add("property", "og:url");
                mUrl.Attributes.Add("content", Url);
                page.Header.Controls.Add(mUrl);
            }

            if (!String.IsNullOrEmpty(Title))
            {
                var mTitle = new HtmlMeta();
                mTitle.Attributes.Add("property", "og:title");
                mTitle.Attributes.Add("content", Server.HtmlEncode(Title));
                page.Header.Controls.Add(mTitle);
            }

            if (!String.IsNullOrEmpty(Description))
            {
                var mDescription = new HtmlMeta();
                mDescription.Attributes.Add("property", "og:description");
                mDescription.Attributes.Add("content", Server.HtmlEncode(Description));
                page.Header.Controls.Add(mDescription);
            }

            if (!String.IsNullOrEmpty(ImageUrl))
            {
                var mImageUrl = new HtmlMeta();
                mImageUrl.Attributes.Add("property", "og:image");
                mImageUrl.Attributes.Add("content", ImageUrl);
                page.Header.Controls.Add(mImageUrl);
            }

        }

        #endregion

    }
}

部分ASPX页码:

<MyWebsite:AddThisSocial runat="server" ID="ProductAddThisSocial" Description="<%# Eval("Description") %>" ImageUrl="<%= GetImageUrl() %>" Facebook="True" Twitter="True" Google="True" CompactButton="True" Pinterest="True" />

上述方法不起作用,而如果我将静态字符串设置为控件,则可以正常工作。

<MyWebsite:AddThisSocial runat="server" ID="ProductAddThisSocial" Description="My Description" ImageUrl="My Image Url" Facebook="True" Twitter="True" Google="True" CompactButton="True" Pinterest="True" />

我已经完成了对此的调试,并且在网上阅读此内容后这不起作用是有道理的。在父页面的加载方法时,不会初始化控件属性。我也尝试将属性设置为可绑定并将page_load更改为page_prerender方法而没有任何运气。我需要找到一个解决方案,这将允许我不知道在代码隐藏中设置控件属性。有人可以帮忙吗?

谢谢, Hiral

1 个答案:

答案 0 :(得分:1)

看看这些帖子。基本上,您需要使用Render方法和ViewState来获取属性绑定:

ASP.NET User Control : can't initialize a user control property using Eval("...")

Stumped With Custom Property on User Control