Solved

loop thru HTML generic controls

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 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