Click here to Skip to main content
6,306,412 members and growing! (17,006 online)
Email Password   helpLost your password?
Desktop Development » Combo & List Boxes » Combo and List boxes     Intermediate License: The Code Project Open License (CPOL)

The Localizable Dropdown for ASP.NET 2.0

By Shahid Syed

Simple way of binding dropdown control to resource files
C# 2.0.NET 2.0, ASP.NET, WebForms
Version:4 (See All)
Posted:1 Jan 2009
Views:5,220
Bookmarked:9 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
2 votes for this article.
Popularity: 0.90 Rating: 3.00 out of 5

1

2
2 votes, 100.0%
3

4

5

Introduction

The code shows a simple way of binding ListItem based controls such as dropdown to a resource file. 

Background 

The idea is to store data in XML format instead of name value collection and bind it to .NET controls using XMLDataSource object.  

Using the Code  

I've added a string resource "MyCountryList" that contains XML data for dropdown control.  

Resource.en-US.resx

<countries>
  <country text="Pakistan" value="PK"/>
  <country text="Iran" value="IR"/>
  <country text="Afghanistan" value="AF"/>
</countries>

Resource.es-MX.resx

<countries>
  <country text="Pakistan-Spanish" value="PK"/>
  <country text="Iran-Spanish" value="IR"/>
  <country text="Afghanistan-Spanish" value="AF"/>
</countries>

Default.aspx

<form id="form1" runat="server">
        <asp:HyperLink ID="HyperLink1" runat="server" 
		NavigateUrl="~/Default.aspx?lang=esp">Esp</asp:HyperLink>
          
        <asp:HyperLink ID="HyperLink2" runat="server" 
		NavigateUrl="~/Default.aspx?lang=eng">Eng</asp:HyperLink><br />
        <br />
        <asp:Localize ID="Localize1" runat="server" 
		Text="<%$ Resources:Resource, Greeting%>"></asp:Localize>
        <br />
        <asp:XmlDataSource ID="XmlDataSource1" runat="server"></asp:XmlDataSource>
        <asp:DropDownList ID="DropDownList1" runat="server" >
        </asp:DropDownList>
</form>

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        // loading xml from resource file into xmldatasource object
        XmlDataSource1.Data = Resources.Resource.MyCountryList;
        XmlDataSource1.EnableCaching = false;
        XmlDataSource1.DataBind();        

        DropDownList1.DataSourceID = "XmlDataSource1";
        DropDownList1.DataTextField = "text";
        DropDownList1.DataValueField = "value";
        DropDownList1.DataBind();
    }
   //The setting up the culture
    protected override void InitializeCulture()
    {
        string lang = "en-US";
        if (Request.QueryString["lang"] != null)
        {
            switch (Request.QueryString["lang"])
            {
                case "esp":
                    lang = "es-MX";
                    break;
                case "eng":
                    lang = "en-US";
                    break;
            }
        }
        System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(lang);
        System.Threading.Thread.CurrentThread.CurrentCulture = ci;
        System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
        base.InitializeCulture();
    } 

The lines in italics show the 2 ways to bind to resource files. You can also download the source code attached to this article.

History

  • 1st January, 2009: Initial post

License

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

About the Author

Shahid Syed


Member

Occupation: Software Developer (Senior)
Company: Mobile Complete Inc. (DeviceAnywhere)
Location: Pakistan Pakistan

Other popular Combo & List Boxes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralProblems with Japanese PinmemberNickShelomanov23:49 11 Jan '09  
GeneralBest Localization Plug-in for Visual Studio. PinmemberAlexander Nesterenko2:34 2 Jan '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Jan 2009
Editor: Deeksha Shenoy
Copyright 2009 by Shahid Syed
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project