Solved

loop thru HTML generic controls

Posted on 2007-11-16
2
766 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code works but how can I download the file? 20 67
Allow space in this pattern 2 47
Authentication of Web Services 3 49
Connection String 16 43
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 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

26 Experts available now in Live!

Get 1:1 Help Now