简体   繁体   中英

Repeater using a user control

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?

<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()

}

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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