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);
}