How to loop through all WebParts controls

There is a way I loop through all controls of my Sharepoint 2013 Visual WebPart, and only them? Something like this:


foreach(var _control in this.Controls)
{
if (_control.typeof() == )
_control.Text = GetTranslation(_control.Text);
if (_control.typeof() == )
_control.InnerText = GetTranslation(_control.InnerText);
}

As you can see, we need to translate all text in a webpart through a function (for specific reasons we cannot use Resources, please avoid comments on that).

=================

  

 

Is it a visual webpart?
– Amal Hashim
Jan 17 ’15 at 13:33

  

 

Yes, it is a visual WebPart, I’ll edit the question to make it more clear, thanks!
– tcbrazil
Jan 17 ’15 at 13:39

=================

1 Answer
1

=================

You can use the control collection using below code

foreach (Control c in Page.Controls)
{
foreach (Control childc in c.Controls)
{
if (childc is TextBox)
{
//Use childc.Text property
}
else if(childc is Label)
{

}

//…
}
}

1

 

Amal, the way you presented I believe it would loop ALL controls in the whole page (Master pages, etc.), and I want to do it only in the WebPart controls.
– tcbrazil
Jan 17 ’15 at 14:34

  

 

tcbrazil: You can try it out. I believe it only give you the control belonging to your particular user control.
– Amal Hashim
Jan 17 ’15 at 14:35

  

 

Yes, it loops through the webpart controls only, tks!
– tcbrazil
Jan 18 ’15 at 1:37