Blog de Francisco Velázquez

otro blog personal

Repeater dentro User Control

leave a comment »

Como crear controles de usuario que se componen de un Repeater y queremos iniciarlos desde fuera.


<%@ Control Language="C#" AutoEventWireup="true"  CodeBehind="ExRepeater.ascx.cs"
Inherits="TestUserControls.ExRepeater" ClassName="ExRepeater" %>

<script runat="server"></script>

<asp:Repeater ID="repeater" runat="server">
<HeaderTemplate>
<div>
<h2>
List</h2>
<h3>
example</h3>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<%#((RepeaterItem) Container).DataItem.ToString() %>
</li>
</ItemTemplate>
<FooterTemplate>
</ul> </div>
</FooterTemplate>
</asp:Repeater>

—-


public partial class ExRepeater : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
repeater.DataBind();
}

public object DataSource
{
get
{
Control c = FindControl("repeater");
return ((Repeater)c).DataSource;
}
set
{
Control c = FindControl("repeater");
((Repeater)c).DataSource = value;
}
}
}

En la página en el fichero aspx (o porque no, otro control) tendremos que referencial al control que contiene el Repeater:

 <%@ Reference Control="~/ExRepeater.ascx" %> 

Y en el código:


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Control c = FindControl("form1");

string[] list = new string[2] { "elem1", "elem2" };
Control uc2 = LoadControl("~/ExRepeater.ascx");
((ExRepeater)uc2).DataSource = list;
c.Controls.Add(uc2);

}
}

Written by fravelgue

March 18, 2010 at 10:39 pm

Posted in Uncategorized

Tagged with , ,

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

%d bloggers like this: