Solved

loop thru HTML generic controls

Posted on 2007-11-16
2
772 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
ID: 20301944
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
ID: 20301979
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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