0

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 with Text properties>)
        _control.Text = GetTranslation(_control.Text);
    if (_control.typeof() == <Control with InnerText properties>)
        _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).

2
  • Is it a visual webpart? Jan 17, 2015 at 13:33
  • Yes, it is a visual WebPart, I'll edit the question to make it more clear, thanks!
    – tcbrazil
    Jan 17, 2015 at 13:39

1 Answer 1

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)
        {
            ...
        }

        //...
    }
}
3
  • 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, 2015 at 14:34
  • tcbrazil: You can try it out. I believe it only give you the control belonging to your particular user control. Jan 17, 2015 at 14:35
  • Yes, it loops through the webpart controls only, tks!
    – tcbrazil
    Jan 18, 2015 at 1:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.