The code shows a simple way of binding ListItem based controls such as dropdown to a resource file.
The idea is to store data in XML format instead of name value collection and bind it to .NET controls using XMLDataSource object.
I've added a string resource "MyCountryList" that contains XML data for dropdown control.
<countries>
<country text="Pakistan" value="PK"/>
<country text="Iran" value="IR"/>
<country text="Afghanistan" value="AF"/>
</countries>
<countries>
<country text="Pakistan-Spanish" value="PK"/>
<country text="Iran-Spanish" value="IR"/>
<country text="Afghanistan-Spanish" value="AF"/>
</countries>
<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>
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.
| You must Sign In to use this message board. | |||||||||||
|
|||||||||||
|
|||||||||||
|
|||||||||||