简体   繁体   中英

How to pass in the entire object into a function in a repeater?

In my codebehind I have:

List<Products> products = 
rptr.Datasource = products;


protected string GetVariants(Product product)
{
  //
}

In my repeater I have:

<itemTemplate>

<li>  <%# Eval("Name") %>
<li>  <%# GetVariants(?????????????) %>

</itemTemplate>

How do I pass the product object to the GetVariants method?

(Product)Container.DataItem应该可以解决问题

Instead of calling back into a method from your markup page, what about creating a new class for binding your Repeater? This avoids the issue all together.

public class RepeaterItem
{
    public string Name;
    public string Variants;
}

And then bind the Repeater with a new datasource:

List<Products> products = //Get Products
List<RepeaterItem> repeaterItems = new List<RepeaterItem>();

foreach(var product in products)
{
    repeaterItems.Add(
        new RepeaterItem 
            { 
                Name = product.Name,
                Variants = GetVariants(product)
            });
}

rptr.DataSource = repeaterItems;

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