Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML:
ASP.NET
<div  runat="server" ID="divCategory">
  <div class="item">
    <div class="col-lg-12">
        <div class="col-sm-3">
            <img src="1.png" /><br />
        </div>
        <div class="col-sm-3">
            <img src="1.png" /><br />
       </div>
       <div class="col-sm-3">
          <img src="1.png" /><br />
       </div>
       <div class="col-sm-3">
          <img src="1.png" /><br />
       </div>
    </div>
    <div class="col-lg-12">
        <div class="col-sm-3">
            <img src="1.png" /><br />
        </div>
        <div class="col-sm-3">
            <img src="1.png" /><br />
       </div>
       <div class="col-sm-3">
          <img src="1.png" /><br />
       </div>
       <div class="col-sm-3">
          <img src="1.png" /><br />
       </div>
    </div>
  </div>
</div>


i need to write the code as above in c# code behind, this is how i get so far, can someone point out where i did wrong?

C#
HtmlGenericControl divItem = new HtmlGenericControl("div");
  divItem.Attributes["class"] = "item";

  HtmlGenericControl divColumnH = new HtmlGenericControl("div");
  divColumnH.Attributes["class"] = "col-lg-12";

  HtmlGenericControl divColumnD = new HtmlGenericControl("div");
  divColumnD.Attributes["class"] = "col-sm-3";

  Image img = new Image();
  img.ImageUrl = "1.png";
  img.Visible = true;

  divCategory.Controls.Add(divItem);
  divItem.Controls.Add(divColumnH);
  for (int i = 0; i < 4; i++)
  {                
      divColumnH.Controls.Add(divColumnD);
      divColumnD.Controls.Add(img);
  }
Posted
Comments
ZurdoDev 21-Jul-14 13:03pm    
Where are you stuck?
melvintcs 21-Jul-14 20:18pm    
the for loop, it only shows 1 img instead of 4
ZurdoDev 21-Jul-14 20:24pm    
You keep adding the same control over and over. In your loop, instead create a new control each time and add that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900