Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to get the names of each web controls while loading the page in asp.net.

Please help me.

The "this" object shows all the controls but when I tried this.form.controls; I'm getting only html controls

Thank you
Posted
Updated 9-Jan-11 21:39pm
v2
Comments
Sandeep Mewara 10-Jan-11 3:27am    
Question not quite clear.
Dalek Dave 10-Jan-11 3:39am    
Edited for Grammar and Spelling.

Specify runat="server" on all asp.net controls to access it from form.controls.
In the class System.Web.UI.HTMLControl, the attribute is not required.
However, in the class System.Web.UI.WebControl the attribute is required.
 
Share this answer
 
v2
Comments
Sandeep Mewara 10-Jan-11 3:27am    
Comment from OP:
thanks...
i have already added runat=server in each control.
but stil im not getting the sever controls names. For example..i want to get the name of an asp:Label control..how wil i get it when page loading or closing? the name is lisiting udder "this" object..
Dalek Dave 10-Jan-11 3:41am    
Useful info.
I corrected a couple of spelling mistakes, in case you were wondering.
abdu_karami 10-Jan-11 3:42am    
thanks...
i have already added runat=server in each control.
but stil im not getting the sever controls names. For example..i want to get the name of an asp:Label control..how wil i get it when page loading or closing? the name is lisiting udder "this" object..
Anupama Roy 10-Jan-11 3:55am    
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
abdu_karami 10-Jan-11 9:19am    
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.....
Please use following
C#
foreach (Control ctrl in this.Controls)
       {
       }


Thanks,
Imdadhusen
 
Share this answer
 
Comments
Dalek Dave 10-Jan-11 3:41am    
Ah yes, good call, 5.
Anupama Roy 10-Jan-11 3:57am    
Right!

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