Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a web page which first loads master page and then the current page. I want to get all the names of the controls availble on the page.
when i used this.controls or this.form.controls Im getting only the controls from Master page. The current page controls is not vailable.. but when i run line by line..it shows all the controls in the "this" object.. how to get all the names from the "this" object

thak u...........
Posted

You asked this before, and this was given...

C#
foreach (Control ctrl in this.Controls)
       {
       }


*With thanks to Sunasara Imdadhusen


I suggest you try it before asking for a third time.
 
Share this answer
 
Comments
Hiren solanki 10-Jan-11 3:51am    
Well done DD.
abdu_karami 10-Jan-11 4:37am    
thanks..
it works without master page..
but whn master page loads first..it wil read from Master page only..
abdu_karami 10-Jan-11 9:20am    
hi, i tried all these..but its not working...it works in pages which doesnt have master pages. In my project, i ahve master page with 40 controls and child pages comes under the ContentPlaceholder of the master page. when a pages is loaded wth master page..i shold get the names of all the controls from both master and child pages. but it giving only master page controls(40 controls)..is there any other solution to get the control names? thanks for ur previos answer.... thank you.....
I posted same answer to your previous question.I would suggest,if you want to add anything to already asked question,please modify it before reposting it.

The basicic heirachy of controls on a page (which uses MasterPages) is MasterPage then HtmlForm then ContentPlaceHolder and then finally will come all the labels, textboxes, and whatever other controls you seek (note that if these controls contain children of their own, like a GridView, you will need to loop through that control to get all the included controls in it).

foreach (Control masterControl in Page.Controls)
{
    if (masterControl is MasterPage)
    {
        foreach (Control formControl in masterControl.Controls)
        {
            if (formControl is System.Web.UI.HtmlControls.HtmlForm)
            {
                foreach (Control contentControl in formControl.Controls)
                {
                    if (contentControl is ContentPlaceHolder)
                    {
                        foreach (Control childControl in contentControl.Controls)
                        {

                        }
                    }
                }
            }
        }
    }
}


For a specific control use this

if (childControl is Label)
{
    Label newLabel = (Label)childControl;
    newLabel.Text = "You found me!"
}


Source :

http://www.krissteele.net/blogdetails.aspx?id=104[^]
 
Share this answer
 
Comments
Kasson 10-Jan-11 4:53am    
Good Answer Anupama.
Anupama Roy 10-Jan-11 5:14am    
thanks Kasson.
Try this...

C#
private void LoopControls(System.Web.UI.Control ctlParent)
{
    foreach (Control ctrl in ctlParent.Controls)
    {
        if (ctrl.HasControls())
        {
            ///Do Something
            LoopControls(ctrl);
        }
        else
        {
            ///Do Something
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900