C# 如何使用C查找html元素

C# 如何使用C查找html元素,c#,html,asp.net,htmlcontrols,C#,Html,Asp.net,Htmlcontrols,我的.aspx代码中有这一行: *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

我的.aspx代码中有这一行:

*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循环的最后一行中使用的视图。 您可以使用搜索具有指定id参数的控件,如下所示:

Control view = FindControl("view");
//Then add your item to div
view.Controls.Add(item);

这回答了你的问题吗?你到底犯了什么错误?请注意,您确实编写了view.Control而不是view.Controls pluralcode review:您设置了很多item.InnerHtml。我想你指的是刚刚创建的控件,而不是项目
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);
}
Control view = FindControl("view");
//Then add your item to div
view.Controls.Add(item);