【发布时间】:2020-03-12 18:30:18
【问题描述】:
我的.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 - 我看到一门课程做同样的事情并且很成功
- 错误是即使我的 Visual Studio 也找不到我在 for 循环的最后一行中使用的
view。
【问题讨论】:
-
这能回答你的问题吗? c# - reading HTML?
-
你得到的错误究竟是什么?请注意,您确实写了
view.Control而不是view.Controls(复数) -
代码审查:你设置了很多
item.InnerHtml。我认为您的意思是您刚刚创建的控件而不是“项目”
标签: c# html asp.net htmlcontrols