Click here to Skip to main content
14,365,495 members

A localizable dropdown for ASP.NET 2.0

Rate this:
2.80 (5 votes)
Please Sign up or sign in to vote.
2.80 (5 votes)
29 Nov 2007CPOL
This code shows how to do a simple localizable dropdown that sets up the options from suitable text.

Sample image


This code shows how to do a simple localizable dropdown that sets up the options from a suitable text format. I had the usual HTML <select...> with many <option...>s and I wanted to have a localizable dropdown that could catch such data as a list of "<option>" tags or maybe a list of any other data. The problem is that the standard ASP.NET 2.0 dropsown allows getting text from resources, but only for each option, not for whole dropdown control. So the custom dropdown should:

  1. Be simple and easy-to use.
  2. Be localizable (i.e., catch data from resources).
  3. Not request any additional code to write.

Using the code

After some thought and getting advices, I decided that the only good way is to create a successor of the standard dropdown control. The first step is to create our custom dropdown class in the App_Code folder. Before the class declaration, put the following code:

ToolboxData("<{0}:CustomDropDown runat="server">")]

Here we specify the default tag for our control. Then we should add the member for the text to be parsed:

private string innerText;
<Bindable(true), Category("Appearance"), DefaultValue("")>
public string InnerText
       return innerText;
       innerText = value;

Our future property will be appearing in the "Appearance" category and will have a value "". Then we add the Prepare method that parses the text and fills the items of the dropdown list:

public void Prepare()
    if (innerText == string.Empty || innerText == "")
    if (Items.Count > 0)
    string sep = separator == "" ? "@" : separator;
    innerText = innerText.Replace("</option /><option />", sep);
    innerText = innerText.Replace("</option />\r\n<option />", sep);
    innerText = innerText.Replace("</option />", "");
    innerText = innerText.Replace("<option />", "");
    innerText = innerText.Replace("â€"", "-");
    innerText = innerText.Replace("'", "'");
    string[] cc = innerText.Split(new string[] { sep },
    foreach (string c in cc)
        this.Items.Add(new ListItem(c));

It's very simple. It just splits the string, deletes the extra tags (like "<option>") between items, and fills the list. The only thing to describe more is the separator. It allows us to use the source text in different formats. It can be the usual "comma-separated values" or a real list of "<options...></options>" tags. At first, we just replace the source separated by our separator and then split the string. For example, if we have the list of tags, we could use the "," separator, but we will have a problem if our string has the "," symbol itself. In this case, it will be better to use another separator, for example, "@". And such a separator will be the default. We add the separator declaration like:

private string separator = "";
<Bindable(true), Category("Appearance"), DefaultValue("")>
public string Separator
        return separator;

        separator = value;

We call the Prepare method while the page is loading:

protected override void OnLoad(EventArgs e)

Then we should set up the culture. The best place for the code is the page's method InitializeCulture:

protected override void InitializeCulture()
    string lang = "en-US";
    if (Request.QueryString["lang"] != null)
        switch (Request.QueryString["lang"])
            case "rus":
                lang = "ru-RU";
            case "eng":
                lang = "en-US";
    System.Globalization.CultureInfo ci = new
    System.Threading.Thread.CurrentThread.CurrentCulture = ci;
    System.Threading.Thread.CurrentThread.CurrentUICulture = ci;

That is a special method and is called very early before creating any controls. In this example, we set up one of two cultures: English or Russian (you can use as many languages as you want). We toggle the culture by clicking on the links:

<asp:HyperLink ID="HyperLink1" runat="server" 

<asp:HyperLink ID="HyperLink2" runat="server" 


Every link passes the corresponding "lang" parameter. The next step is to add our control to the page:

<ddc:CustomDropDown ID="Countries" 

   InnerText="<%$ Resources:Resource, CountriesCombo%>" 

   runat="server" Width="320px" style="margin-top:5px" 


Don't forget to register our control:

<%@ Register Namespace="CustomControls"  TagPrefix="ddc"  %>

Finally we should add the resource. In the given sample, we are creating three resources files - Resource.resx, Resource.en-US.resx, and in App_GlobalResources. Then in every file, we add a new item named CountriesCombo and put the text in it. Then we compile the project... Voila!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Canada Canada
Ich bin.

Comments and Discussions

GeneralInternationalization via DB Pin
Remy Blaettler15-Feb-07 2:34
memberRemy Blaettler15-Feb-07 2:34 
AnswerRe: Internationalization via DB Pin
sea_caty16-Feb-07 0:57
membersea_caty16-Feb-07 0:57 
GeneralRe: Internationalization via DB Pin
Remy Blaettler19-Feb-07 12:34
memberRemy Blaettler19-Feb-07 12:34 
GeneralRe: Internationalization via DB Pin
sea_caty20-Feb-07 0:08
membersea_caty20-Feb-07 0:08 
GeneralRe: Internationalization via DB Pin
fred_00729-Nov-07 6:17
memberfred_00729-Nov-07 6:17 
GeneralRe: Internationalization via DB Pin
richerm29-Nov-07 4:14
memberricherm29-Nov-07 4:14 
GeneralRe: Internationalization via DB Pin
sea_caty29-Nov-07 22:02
membersea_caty29-Nov-07 22:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 2 Feb 2007


21 bookmarked