views:

33

answers:

2

I have a repeater with which I want to use to show a user control multiple times, populated with data.
Currently, I have this:

<asp:Repeater runat="server" ID="MyRepeater" >
    <ItemTemplate>
        <uc1:MyItems ID="MyItems1" runat="server"  />
    </ItemTemplate>
</asp:Repeater>

My user control has three properties, which I want to populate for each. I currently have this:

protected void Page_Load(object sender, EventArgs e)
{
    MyDataSource.SelectCommand =
                "SELECT Name, Address, Phone " +
                "FROM TestTable ";
    MyDataSource.SelectCommandType = SqlDataSourceCommandType.Text;
    DataView resultsdv = (DataView)MyDataSource.Select(DataSourceSelectArguments.Empty);
    foreach (DataRow dr in resultsdv.Table.Rows)
    {            
        MyItems1.Cust_Name = dr["Name"].ToString();
        MyItems1.Cust_Address = dr["Address"].ToString();
        MyItems1.Cust_Tel = dr["Phone"].ToString();
    }
}

Obviously, this isn’t doing what I want. Is it possible to tell the repeater that I want to populate my user control – either by data binding it, or manually populate it in a way similar to above?

+3  A: 
<asp:Repeater runat="server" ID="MyRepeater" >
    <ItemTemplate>
        <uc1:MyItems ID="MyItems1" MyItems="<%# Eval("Name") %>" ... runat="server"  />
    </ItemTemplate>
</asp:Repeater>

protected void Page_Load(object sender, EventArgs e)
{
    MyDataSource.SelectCommand =
                "SELECT Name, Address, Phone " +
                "FROM TestTable ";
    MyDataSource.SelectCommandType = SqlDataSourceCommandType.Text;
    DataView resultsdv = (DataView)MyDataSource.Select(DataSourceSelectArguments.Empty);
    MyRepeater.DataSource = resultsdv.Table.Rows;
    MyRepeater.DataBind()

}
Alex Reitbort
Thanks - this worked with a slight modification: Cust_Name=<%#Eval("[\"Name\"]") %>
pm_2
+1  A: 

I think that needs some corrections:

<asp:Repeater runat="server" ID="MyRepeater" >
    <ItemTemplate>
        <uc1:MyItems ID="MyItems1" Cust_Name=<%#Eval("Name") %> Cust_Address=<%#Eval("Address")%> Cust_Tel=<%#Eval("Phone")%> runat="server"  />
    </ItemTemplate>
</asp:Repeater>
CyberDude