Solved

loop thru HTML generic controls

Posted on 2007-11-16
2
764 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:-Dman100-
2 Comments
 
LVL 33

Expert Comment

by:raterus
Comment Utility
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.
0
 
LVL 16

Accepted Solution

by:
McExp earned 500 total points
Comment Utility
To access the Controls you will need to recursivly search through each of the NamingContainers in the Page, if you use the function below instead of the OOB FindControl you should see that a reference to your control is returned.

As it is a custom function, the signature is slighlty different.

The code you need to call the function is: -

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

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

    /// <summary>
    /// Returns a control if one by that name exists in the hierarchy of the controls collection of the start control
    /// </summary>
    /// <param name="start"></param>
    /// <param name="id"></param>
    /// <returns></returns>
    public static Control FindControl(Control start, string id)
    {
        Control foundControl;

        if (start != null)
        {
            foundControl = start.FindControl(id);

            if (foundControl != null)
                return foundControl;

            foreach (Control c in start.Controls)
            {
                foundControl = FindControl(c, id);
                if (foundControl != null)
                    return foundControl;
            }
        }
        return null;
    }
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now