-1

I have this line in my .aspx code:

 <div id="view" class="g2" runat="server">
 </div>

but when I want to find this element in C#(aspx.cs) and send data for it, C# cant find it this way:

view.Controls.Add(item);

*item is a html element that I made through this code:

for(int i=0; i<foods.Count; i++)
{
    HtmlGenericControl item = new HtmlGenericControl("div");
    item.Attributes["class"] = "items";
    item.Style["border_bottom"] = "3px solid aqua";

    HtmlImage img = new HtmlImage();
    img.Src = foods[i].picGet();

    HtmlGenericControl mask = new HtmlGenericControl("div");
    item.Attributes["class"] = "mask";

    HtmlGenericControl h2 = new HtmlGenericControl("h2");
    item.InnerHtml = foods[i].fnameGet();

    HtmlGenericControl price = new HtmlGenericControl("span");
    item.Attributes["class"] = "price";
    item.InnerHtml = foods[i].priceGet().ToString() + "ریال";

    HtmlGenericControl desc = new HtmlGenericControl("p");
    item.InnerHtml = foods[i].describeGet();

    mask.Controls.Add(h2);
    mask.Controls.Add(price);
    mask.Controls.Add(desc);

    item.Controls.Add(img);
    item.Controls.Add(mask);

    view.Control.Add(item);
}
  • I have written the namespace System.Web.UI.HtmlControls
  • I saw a course that did the same thing and was successful
  • the error is that even my visual studio cant find the view that i used in the last line of the for loop.
amshakur
  • 11
  • 4

1 Answers1

1

You can use Control.FindControl to search control with the specified id parameter as follow:

Control view = FindControl("view");
//Then add your item to div
view.Controls.Add(item);
Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28