如何遍历控件以获取特定类型的控件? [英] How to loop through controls to get specific type of controls?
问题描述
我有一个像这样的div:
I have a div like this:
<div id="columns" runat="server">
<ul id="column1" class="column" >
<!-- /////////////// -->
</ul>
<ul id="column2" class="column" runat="server">
</ul>
<ul id="column3" class="column" runat="server">
</ul>
<ul id="column4" class="column" runat="server">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</ul>
</div>
在运行时,我在(ul
具有属性runat ="server"
)中创建 listitems(HtmlGenericControl outer_li = new HtmlGenericControl("li");
).
In run time I create listitems(HtmlGenericControl outer_li = new HtmlGenericControl("li");
) in the (ul
which has attribute runat ="server"
).
现在我想做的是:
遍历div id="columns"
以获得每个(具有属性runat ="server"
的ul
),然后遍历每个(ul)以获得每个列表项以保存内容.
Now what i wanna to do is:
looping through div id="columns"
to get each (ul
which has attribute runat ="server"
) then loop through each (ul) to get each list item to save contents .
foreach (Control c in columns.Controls.OfType<HtmlGenericControl>())
{
var ctrl = (HtmlGenericControl)c;
if (ctrl.TagName == "ul" && ctrl.ID != "column1")
{
foreach (Control li in ctrl.Controls.OfType<HtmlGenericControl>())
{
var ctrl_li = (HtmlGenericControl)li;
if (ctrl_li.TagName == "li")
{
string id = ctrl_li.ID;
}
}
}
}
这不起作用,尽管我的页面上有li
,我也根本无法获得"li"
this doesn't work i can't get the 'li' at all although there are li
on my page
protected void CreateBlockOfData(string widget_color, int column_par, string process_name, int block_type, int block_id)
{
HtmlGenericControl outer_li = new HtmlGenericControl("li");
outer_li.Attributes.Add("class", widget_color);
if (column_par == 1)
{
column1.Controls.Add(outer_li);
}
else if (column_par == 2)
{
//uppnl_2.ContentTemplateContainer.Controls.Add(outer_li);
column2.Controls.Add(outer_li);
}
else if (column_par == 3)
{
column3.Controls.Add(outer_li);
}
else if (column_par == 4)
{
column4.Controls.Add(outer_li);
}
////////////////////////////////////////////////////////////
HtmlGenericControl div_head = new HtmlGenericControl("div");
div_head.Attributes.Add("class", "widget-head");
outer_li.Controls.Add(div_head);
////////////////////////////////////////////////////////////
HtmlGenericControl h3 = new HtmlGenericControl("h3");
div_head.Controls.Add(h3);
///////////////////////////////////////////////////////////
Label lbl_process_name = new Label();
lbl_process_name.Text = process_name.TrimEnd();
h3.Controls.Add(lbl_process_name);
///////////////////////////////////////////////////////////
HtmlGenericControl div_content = new HtmlGenericControl("div");
div_content.Attributes.Add("class", "widget-content");
outer_li.Controls.Add(div_content);
////////////////////////Data//////////////////////////////
Control crl_data = FormTheData(block_type, block_id);
PlaceHolder1.Controls.Add(crl_data);
crl_data.DataBind();
div_content.Controls.Add(crl_data);
}
推荐答案
我对此进行了测试,并手动添加了一些<li>
.如果在标签中设置runat="server"
,我可以让它们显示出来.因此,您用于生成和添加ListItems的代码不会使用runat来创建它们.
I tested this and added some <li>
's manually. I could get them to show up if I set runat="server"
in their tag. So the code you're using to generate and add the ListItems is not creating them with the runat.
修改
您需要考虑使用MAW74656建议使用的实际ASP .NET控件(例如DataGrid或Repeater或BulletedList)代替HTML <ul>
.
Edit
You made need to consider using an actual ASP .NET Control such as a DataGrid or Repeater or BulletedList as MAW74656 suggested instead of HTML <ul>
's.
这篇关于如何遍历控件以获取特定类型的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!