Kanes
Kanes

Reputation: 105

How to loop div?

I am trying to loop few div's in aspx. This the code I would like to loop:

<div class="col-md-3 product-men">
    <div class="men-pro-item simpleCart_shelfItem">
        <div class="men-thumb-item">
            <img src="uploadImage/3.png" class="pro-image-front" />
            <img src="uploadImage/3.png" class="pro-image-back" />
        </div>
        <div class="item-info-product ">
            <h4><a href="single.html">Tie Clip</a></h4>
            <div class="info-product-price">
                <span class="item_price">RM100</span>
            </div>
            <a href="#" class="item_add single-item hvr-outline-out button2">Add to cart</a>
        </div>
    </div>
</div>

The page looks like below:

enter image description here

the cart need to loop. I have tried using c# as below:

StringBuilder cart = new StringBuilder();

for (int x = 0; x < 3; x++) {
 cart.Append(" <div class=\"col - md - 3 product - men\">");
 cart.Append("<div class=\"men - pro - item simpleCart_shelfItem\">");
 cart.Append("<div class=\"men - thumb - item\">");
 cart.Append("<img src = uploadImage/1.jpg class='pro - image - front />");
 cart.Append("<img src = uploadImage/1.jpg class=pro - image - back />");
 cart.Append("</div>");
 cart.Append("  <div class=\"item - info - product \">");
 cart.Append("<h4><a href = \"single.html\" > Tie Clip</a></h4>");
 cart.Append("<div class=\"info - product - price\">");
 cart.Append("<span class=\"item_price\">RM100</span>");
 cart.Append("</div>");
 cart.Append("<a href = \"#\" class=\"item_add single-item hvr-outline-out button2\">Add to cart</a>");
 cart.Append("</div>");
 cart.Append("</div>");
 cart.Append("</div>");
}

Literal1.Text = cart.ToString();

This don't seem work out.. Any idea how to get this cart loop?

Upvotes: 0

Views: 1838

Answers (3)

VDWWD
VDWWD

Reputation: 35544

You would normally use a asp.net Control for that, like a Repeater.

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <div class="col-md-3 product-men">
            <div class="men-pro-item simpleCart_shelfItem">
                <div class="men-thumb-item">
                    <img src="uploadImage/<%# Eval("product_image_front") %>" class="pro-image-front" />
                    <img src="uploadImage/<%# Eval("product_image_back") %>" class="pro-image-back" />
                </div>
                <div class="item-info-product ">
                    <h4><a href="single.html"><%# Eval("product_name") %></a></h4>
                    <div class="info-product-price">
                        <span class="item_price"><%# Eval("product_price") %></span>
                    </div>
                    <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("product_id") %>' runat="server" CssClass="item_add single-item hvr-outline-out button2">Add to cart</asp:LinkButton>
                </div>
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>

Then in code behind you would assign a DataSource to the Repeater.

Repeater1.DataSource = source;
Repeater1.DataBind();

Upvotes: 0

Jackson V Jose
Jackson V Jose

Reputation: 29

Code in aspx Page <asp:Literal ID="ltrMessage" runat="server"></asp:Literal>

Code Snippet in cs page.

 for(int i=0;i<limit;i++)   
{   
ltrMessage.Text+=@"<div class='col-md-3 product-men'>   
    <div class='men-pro-item simpleCart_shelfItem'>   
        <div class='men-thumb-item'>   
            <img src='uploadImage/3.png' class='pro-image-front' />   
            <img src='uploadImage/3.png' class='pro-image-back' />   
        </div>    
        <div class='item-info-product '>    
            <h4><a href='single.html'>Tie Clip</a></h4>    
            <div class=''info-product-price'>    
                <span class='item_price'>RM100</span>    
            </div>    
            <a href='#' class='item_add single-item hvr-outline-out    button2'>Add to cart</a>   
        </div>    
    </div>    
</div>";   
}

Upvotes: 0

Alrehamy
Alrehamy

Reputation: 316

You should use Embedded Code Blocks in the .aspx itself instead of .cs.

Upvotes: 1

Related Questions