Sunnygurke
Sunnygurke

Reputation: 117

Dynamic creation of divs

I want to create dynamic divs in a ASP.NET application.

protected void Page_Load(object sender, EventArgs e)
{
    List<produkt> produkte = Repository.GetProductsRanged(0, 9).ToList();
}

As you can see, there is a of products (10 elements) and I want to put some of there attributes in divs. Every product should have it owns div. How can I do this dynamicaly?

Thanks in advance!

Upvotes: 0

Views: 513

Answers (2)

Carl Sharman
Carl Sharman

Reputation: 4855

If you don't want to use a repeater:

foreach (produkt prod in produkte)
{
    HtmlGenericControl div = new HtmlGenericControl("div");
    div.Attributes["one"] = "one";
    div.Attributes["two"] = "two";
    this.Form.Controls.Add(div);
}

Upvotes: 0

Arion
Arion

Reputation: 31249

You can use a Repeater

ASPX

<asp:Repeater ID="rep" runat="server">
    <ItemTemplate>
       <div>
         SomeValue
       </div>
    </ItemTemplate>
</asp:Repeater>

Code behind

rep.DataSource=Repository.GetProductsRanged(0, 9).ToList();
rep.DataBind();

Upvotes: 3

Related Questions