Link to home
Start Free TrialLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

loop thru HTML generic controls

I have a series of html labls on my page with the runat="server" added to the tag.

Each <label> tag has an id and I created an array of the id names.  I am trying to loop thru the array and find the labels on the page and perform an action.

This is what I tried, but it does not find the element on the page:

protected void Find()
    {
        string[] controls = { "a", "b", "c", "d" };

        foreach (string ctrl in controls)
        {
            HtmlGenericControl lbl = (HtmlGenericControl)Page.FindControl(ctrl);
            lbl.Visible = false;
            //Response.Write(lbl);
        }
    }

How can I loop thru these page elements by their id?

In javascript, it was just a matter of using getElementById, and I thought I could use FindControl with .net, but it doesn't seem to work.

Thanks for any help.
Avatar of raterus
raterus
Flag of United States of America image

That code looks as if it should work for me.  The only thing I can guess is your label controls are nested deeper in a page inside a control that extends the naming convention one step deeper, so Page.FindControl won't work.  You need to find this inner control and use the findcontrol there.

If you posted your aspx page, I could tell you what you could try.
ASKER CERTIFIED SOLUTION
Avatar of McExp
McExp
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial