Hi i was given some coding in my institute which I am unable to understand.It is regarding creating user defined drop down list of languages spoken that are supported by os...can you please explain me the coding of it clearly....
public class CultureDropdown : DropDownList
{
protected override void OnLoad(EventArgs e)
{
if (!Page.IsPostBack)
{
SortedList lst = getAvailableCultures();
DataSource = getAvailableCultures();
DataValueField = "Value";
DataTextField = "Key";
DataBind();
}
}
private SortedList getAvailableCultures()
{
SortedList results = new SortedList();
string[] languages = ((string)((IDictionary)
WebConfigurationManager.GetSection("supportedLanguages"))["languages"]).Split(',');
foreach (string language in languages)
{
foreach (CultureInfo specific in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
if (specific.TwoLetterISOLanguageName == language)
{
results.Add(specific.DisplayName, specific.Name);
}
}
}
return results;
}
Thanks in advance
Loading
Amit ChoudharyPosted Mar 20, 2010, 2:57 AM
here you function
private SortedList getAvailableCultures()
is providing you the available languages that your project support. This information is fetched from your configuration section of your project.
string[] languages = ((string)((IDictionary)
WebConfigurationManager.GetSection("supportedLanguages"))["languages"]).Split(',');
this line is doing the same thing. but this is not provide the language names so have to get the Language names from CultureInfo class
foreach (CultureInfo specific in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
if (specific.TwoLetterISOLanguageName == language)
{
results.Add(specific.DisplayName, specific.Name);
}
}
This part is fetching the language names from the culture.
Now in your first function
protected override void OnLoad(EventArgs e)
Calling the second function and getting the collection of language in SortedList type collection
SortedList lst = getAvailableCultures();
and once the SortedList is instanced following line bind the data to DropDownlist:
DataSource = getAvailableCultures();
DataValueField = "Value";
DataTextField = "Key";
DataBind();
hope you got better explained.
Please mark as answer if it helps you.