C# 在ASP.NET中本地化动态绑定的DropDownList

C# 在ASP.NET中本地化动态绑定的DropDownList,c#,asp.net,resx,C#,Asp.net,Resx,我有一个dropdownlist,它与数据库表“countries”绑定 dropdownlist将列countryname作为其datatextfield,将列countrycode作为datavaluefield。 假设此表有3个条目,其各自的列值如下所示。 印度,在 德国,德国 美国,美国 我正在将文化更改为de de 我知道对单个静态文本使用resx文件。 现在,我如何将多个datatextfield项同时本地化为de。要做到这一点,您需要将表行集合与该值上的资源文件中的值连接起来,然后

我有一个dropdownlist,它与数据库表“countries”绑定
dropdownlist将列countryname作为其datatextfield,将列countrycode作为datavaluefield。
假设此表有3个条目,其各自的列值如下所示。

印度
德国德国
美国美国

我正在将文化更改为de de
我知道对单个静态文本使用resx文件。
现在,我如何将多个datatextfield项同时本地化为de

要做到这一点,您需要将表行集合与该值上的资源文件中的值连接起来,然后将数据打包到自定义类型中,并将
DropDownList
绑定到该类型中。因此:

var tableRows = table.Rows.Cast<DataRow>();
var resourceStrings = YourResourceClass.ResourceManager
        .GetResourceSet(culture: CultureInfo.CurrentUICulture,
            createIfNotExists: false,
            tryParents: true)
        .Cast<DictionaryEntry>();

var data = tableRows
        .Join(resourceStrings,
            row => row["countrycode"].ToString(),
            resource => resource.Key,
            (row, resource) => new KeyValuePair<string, string>(
                row["countrycode"].ToString(),
                resource.Value.ToString()))
        .ToArray();
并绑定数据源:

ddl.DataSource = data;
编辑

要在不使用LINQ的情况下获得相同的结果,您需要将资源加载到字典中,然后迭代Countries表以构建数据源;大概是这样的:

var resources = new Dictionary<string,string>();
var resourceSet = YourResourceClass.ResourceManager.GetResourceSet(
    CultureInfo.CurrentUICulture, false, true);
foreach(DictionaryEntry kvp in resourceSet)
{
    resources[kvp.Key] = kvp.Value.ToString();
}

var dataSource = new List<KeyValuePair<string, string>>();
foreach(DataRow in table.Rows)
{
    var countryCode = row["countrycode"].ToString();
    if(resources.ContainsKey(countryCode))
    {
        dataSource.Add(new KeyValuePair<string, string>(
            countryCode,
            resources[contryCode]));
    }
}
var resources=newdictionary();
var resourceSet=YourResourceClass.ResourceManager.GetResourceSet(
CultureInfo.CurrentUICulture,false,true);
foreach(字典在资源集中输入kvp)
{
resources[kvp.Key]=kvp.Value.ToString();
}
var dataSource=新列表();
foreach(table.Rows中的数据行)
{
var countryCode=行[“countryCode”]。ToString();
if(resources.ContainsKey(国家代码))
{
添加(新的KeyValuePair)(
国家代码,
资源[控制代码]);
}
}

接下来,将
数据源
绑定到您的DropDownList,一切就绪

由于您的数据来自数据库表,也就是说,它可以更改,为什么不将本地化字符串也保留在数据库中?我希望这些是主值。对于主值,您可以使用映射的资源文件或在DB中存储本地化主数据。我正在寻找方法。我必须在很多地方使用这个。所以我不认为增加本地化专栏是个好主意。谢谢..我可以不用Linq实现吗?(因为我目前没有在我的项目中使用它)。。谢谢@RePierre。。
var resources = new Dictionary<string,string>();
var resourceSet = YourResourceClass.ResourceManager.GetResourceSet(
    CultureInfo.CurrentUICulture, false, true);
foreach(DictionaryEntry kvp in resourceSet)
{
    resources[kvp.Key] = kvp.Value.ToString();
}

var dataSource = new List<KeyValuePair<string, string>>();
foreach(DataRow in table.Rows)
{
    var countryCode = row["countrycode"].ToString();
    if(resources.ContainsKey(countryCode))
    {
        dataSource.Add(new KeyValuePair<string, string>(
            countryCode,
            resources[contryCode]));
    }
}