Databind或将对象传递给Web用户控件属性.net

时间:2013-11-07 09:51:28

标签: c# asp.net data-binding

老实说,我不确定数据绑定是否是实现此目的的正确技术,所以如果有人能够启发我,我将非常感激。

基本上我要做的就是将当前页面中的对象传递给Web用户控件(代码简化):

ExamplePage.aspx

<div>
    <EC:AttachmentsView ID="AttachmentsView1" Attachments=<%# this.PageAttachments %> runat="server" />
</div>

ExamplePage.aspx.cs

public partial class ExamplePage : ProductBase
{
    private LinkItemCollection _pageAttachments;
    public LinkItemCollection PageAttachments
    {
        get { return _pageAttachments; }
    }

    public ExamplePage()
    {
        this.Load += new EventHandler(this.Page_Load);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Accessing and assigning attachments (EPiServer way)
        _pageAttachments = CurrentPage["Attachments"] as LinkItemCollection ?? new LinkItemCollection();
    }
}

附件视图控件包含AttachmentLabel属性的setter和getter。

AttachmentsView.ascx.cs

namespace Example.Controls
{
    [ParseChildren(false)]
    public partial class AttachmentsView : EPiServer.UserControlBase
    {
        private string _label;
        public string Label
        {
            get { return _label; }
            set { _label = value; }
        }

        private LinkItemCollection _attachments;
        public LinkItemCollection Attachments
        {
            get { return _attachments; }
            set { _attachments = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (_attachments == null)
            {
                _attachments = CurrentPage["DefaultAttachments"] as LinkItemCollection ?? new LinkItemCollection();
            }
        }
    }
}

我正处于将期望来自ExamplePage的页面附件传递到AttachmentsView控件但_attachments属性为null的阶段。

我正在尝试做什么?数据绑定是正确的技术,如果有的话,是否有人知道解释概念的材料比可怕的MSDN文档更容易?

我知道我可以通过编程方式渲染控件来实现这一目标,但我想首先尝试这种方法。

0 个答案:

没有答案