我的.aspx
代码中有这一行:
<div id="view" class="g2" runat="server">
</div>
但是当我想在 C#( aspx.cs
) 中找到这个元素并为其发送数据时,C# 无法通过这种方式找到它:
view.Controls.Add(item);
*item 是我通过以下代码制作的 html 元素:
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);
}
- 我已经写了命名空间
System.Web.UI.HtmlControls
- 我看到一门课程做同样的事情并且很成功
- 错误是即使我的视觉工作室也找不到
view
我在 for 循环的最后一行中使用的那个。