How to loop through controls to get specific type

2019-06-25 14:29发布

问题:

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>

In run time I create listitems(HtmlGenericControl outer_li = new HtmlGenericControl("li");) in the (ul which has attribute runat ="server").

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 .


EDIT: according to answers:

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;
                    }
                }
            }
        }

this doesn't work i can't get the 'li' at all although there are li on my page


Edit 2:

 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);
        }

回答1:

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.

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.



回答2:

Maybe you could change your list to use the ASP listitem control, which would allow you to...

Try something like this. Using is to cast a control for testing is efficient.

  foreach (Control c in Page.Controls)
    {
        if (c is ListItem)
        {
            //do something with the control
        }
    }