1
\$\begingroup\$

I would like to optimize the below code The only difference is the datatypes RadioButton, Label, and Button. Outside the method I have an loop that iterates over all the controls in the aspx-page. I have the limitation of .NET 3.5. The code is intended to set language relevant strings on the controls of a certain type in a aspx-page without having to name each control.

using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
...
public partial class MyUserControl : UserControl
...

    private static void SetTextOnControls(MyLanguageTexts keys, ControlCollection theControls)
    {
        foreach (Control control in theControls)
        {
            SetTextOnControl(keys, control);
        }
    }

    private static void SetTextOnControl(MyLanguageTexts keys, Control control)
    {

        if (control is RadioButton)
        {
            try
            {
                (control as RadioButton).Text = SPContext.Current.Web.Locale.LCID == 1033 ?
                    DataBinder.Eval(keys.en, control.ID).ToString() :
                    DataBinder.Eval(keys.sv, control.ID).ToString();
            }
            catch (Exception)
            {
                (control as RadioButton).Text = "Key not found: " + control.ID;
            }
        }
        else if (control is Label)
        {
            try
            {
                (control as Label).Text = SPContext.Current.Web.Locale.LCID == 1033 ?
                DataBinder.Eval(keys.en, control.ID).ToString() :
                DataBinder.Eval(keys.sv, control.ID).ToString();
            }
            catch (Exception)
            {
                (control as Label).Text = "Key not found: " + control.ID;
            }
        }
        else if (control is Button)
        {
            try
            {
                (control as Button).Text = SPContext.Current.Web.Locale.LCID == 1033 ?
                DataBinder.Eval(keys.en, control.ID).ToString() :
                DataBinder.Eval(keys.sv, control.ID).ToString();
            }
            catch (Exception)
            {
                (control as Button).Text = "Key not found: " + control.ID;
            }
        }

        SetTextOnControls(keys, control.Controls);
    }
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Try using the dynamic keyword (.NET 4.5). Since you just updated the question with information that you are limited to 3.5, I suggest using reflection to get the Text property of the control.

var textAwareControl = (dynamic)control;
try
{
    textAwareControl.Text = SPContext.Current.Web.Locale.LCID == 1033 ?
    DataBinder.Eval(keys.en, control.ID).ToString() :
    DataBinder.Eval(keys.sv, control.ID).ToString();
}
catch (RuntimeBinderException) {
    // throw;
    // throw, or ignore, or make sure to only dispatch compatible controls to this method
}
catch (Exception)
{
    textAwareControl.Text = "Key not found: " + control.ID;
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ ...we usually try to avoid answering off-topic or unclear questions as their usefulness is very limited. OP should make some effort and provide enough context so that the purpose of the code is clear and there is enough working code ;-] \$\endgroup\$
    – t3chb0t
    Commented May 26, 2019 at 10:43
  • 1
    \$\begingroup\$ @t3chb0t He did edit his post after your remarks, so I will keep my answer online. But I get your point, a question should strive a minimum of quality requirements. \$\endgroup\$
    – dfhwze
    Commented May 26, 2019 at 10:45
  • \$\begingroup\$ That being said, he just updated with information about v3.5. =] \$\endgroup\$
    – dfhwze
    Commented May 26, 2019 at 10:46

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