How To Embed A UserControl In A Repeater

Page ASPX

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="ControlInRepeater.aspx.cs" Inherits="com.dereksandbox.ui.Pages.ControlInRepeater" %>

<%@ Register Src="../Controls/EmbeddedControl.ascx" TagName="EmbeddedControl" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Repeater ID="Repeater1" runat="server" 
        onitemdatabound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <uc1:EmbeddedControl ID="EmbeddedControl1" runat="server" />
        </ItemTemplate>
    </asp:Repeater>
</asp:Content>

Page Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.dereksandbox.bl.ServiceManagers;
using com.dereksandbox.bl.Entities;
using com.dereksandbox.ui.Controls;

namespace com.dereksandbox.ui.Pages
{
    public partial class ControlInRepeater : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Repeater1.DataSource = PersonServiceManager.getPeople();
            Repeater1.DataBind();
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            
            
            // This event is fired each time a new Person control is created in the repeater

            EmbeddedControl ctrl = (EmbeddedControl)e.Item.FindControl("EmbeddedControl1");
            ctrl.Person = (PersonEntity)e.Item.DataItem;           
           

        }
    }
}

Control ASCX

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="EmbeddedControl.ascx.cs"
    Inherits="com.dereksandbox.ui.Controls.EmbeddedControl" %>
<asp:Label ID="lblName" runat="server" Text="lblName"></asp:Label>
<asp:Label ID="lblWeight" runat="server" Text="lblAge"></asp:Label>
<br />

Control CodeBehind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.dereksandbox.bl.Entities;

namespace com.dereksandbox.ui.Controls
{
    public partial class EmbeddedControl : System.Web.UI.UserControl
    {
        public PersonEntity Person { get; set; }


        protected void Page_Load(object sender, EventArgs e)
        {
            lblName.Text = Person.Name;
            lblWeight.Text = Person.Weight.ToString();
          
        }

      
    }
}
Advertisements

One thought on “How To Embed A UserControl In A Repeater

  1. Hey Derek. I am confused. I’m from the WPF world and trying to learn binding in aspx.

    Where is the Binding taking place. It seems that you are actually setting the properties in the Page_Load and not using data binding at all.

    See the way I understand Binding is that the DataSource only needs to have the property names. The Type of the DataSource shouldn’t matter, just the property names.

    In this case you have Person.Name and Person.Weight. If I had any object, say for example BagOfGrain. With BagOfGrain.Name and BagOfGrain.Weight, I should be able to use this same control. However, obviously your control is tightly coupled to Person and not bound to a property of a DataSource.

    Is this just how aspx works? DataBinding isn’t really there?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s