0

I am building a web page that lists products, using aspx webform. For that, in a user control corresponding to my list, I am looping over my products and injecting one new user control by product:

        foreach (Product p in this.Products)
        {
            ucProductItem.product = p;
    %>
    <uc:ucProductItem runat="server" ID="ucProductItem" />
    <%
        }
    %>

This works fine and I am OK with that... BUT not totally because I find this quite ugly and messy; I don't like the mixing of markup and code in the template, and I try to use markup as much as possible (and I have this problem all over my project).

Thus, I would like to pass the product p to the new ucProductItem user control via the markup, and I tried naturaly something like:

<uc:ucProductItem runat="server" ID="ucProductItem" product=p />

I know this is possible for primitive types such as strings and integers, but I can not figure out how to do it with objects.

Is that possible ? And how ?

woodykiddy
  • 6,074
  • 16
  • 59
  • 100
moudug
  • 209
  • 1
  • 3
  • 13
  • 2
    Premise: you are using webforms in not standard way. In webforms you can create ascx (or directly in the page) containing a GridView. In your case, you can add a public Property Product UCProduct in your UcProductItem and assign it in DataBound event of your container control. I need to see more code for more help: how the Product property will be used? – Emanuele Jun 19 '19 at 08:51
  • Hi Emanuele, I was not aware of the GridView stuff (yes, i am a newbie in webforms) and I am heading straight to check that. The Product propertie will be used to display a "card" with a brief description of the product (like in all commercial sites on the web). – moudug Jun 24 '19 at 09:00
  • 1
    I suggest to read about GridView (if you need pagination) or Repeater control. – Emanuele Jun 25 '19 at 10:39
  • Question already asked and resolved ! https://stackoverflow.com/questions/20136479/user-control-inside-repeater – moudug Jun 25 '19 at 11:23

0 Answers0