简体   繁体   English

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

[英]Binding dynamic properties to user control

Hi fellow stackoverflowers (if that is a word!), 大家好,stackoverflowers(如果这是一个字!),

I am trying to have a user control with dynamic properties but can't get it to work the way I want. 我正在尝试使用具有动态属性的用户控件,但无法使其按我想要的方式工作。 The purpose of the control is to have generic AddThis widget that designers can put inside ASPX without developers having to do anything in ASPX.CS. 该控件的目的是拥有通用的AddThis小部件,设计人员可以将其放入ASPX中,而开发人员无需在ASPX.CS中进行任何操作。 Previously, I have only set the dynamic properties of user control inside codebehind of ASPX page, but now it seems too much hassle to get both programmers and designers involved in simple tasks like this. 以前,我只在ASPX页面的代码内部设置了用户控件的动态属性,但是现在让程序员和设计人员参与这样的简单任务似乎太麻烦了。

This is my code: 这是我的代码:

User Control Code (ASCX): 用户控制代码(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 -->

User Control Code (ASCX.CS): 用户控制代码(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

    }
}

Part of ASPX Page Code: ASPX页面代码的一部分:

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

The above doesn't work, whereas if I set static strings to the controls, it works. 上面的方法不起作用,但是如果我将静态字符串设置为控件,它将起作用。

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

I have done debugging of this and it makes sense that this doesn't work after reading about this on the web. 我已经对此进行了调试,并且在网络上阅读了此内容后,这是行不通的。 The control properties aren't initialised at the time of the parent page's load method. 在父页面的加载方法时,控件属性未初始化。 I have also tried setting properties to bindable and changing the page_load to page_prerender method without any luck. 我也尝试过将属性设置为可绑定,并将page_load更改为page_prerender方法,但没有任何运气。 I need to find a solution to this that will allow me to not have control properties set in code-behind somehow. 我需要找到一个解决方案,使我不必在代码背后设置控件属性。 Can anyone help? 有人可以帮忙吗?

Thanks, Hiral 谢谢,希拉尔

Have a look at these posts. 看看这些帖子。 Basically you need to utilise Render method and ViewState to get your properties binding: 基本上,您需要利用Render方法和ViewState来绑定属性:

ASP.NET User Control : can't initialize a user control property using Eval("...") ASP.NET用户控件:无法使用Eval(“ ...”)初始化用户控件属性

Stumped With Custom Property on User Control 在用户控件上显示自定义属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM