国家/地区代码的下拉列表 [英] dropdown list for countries code

查看:267
本文介绍了国家/地区代码的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为注册页面生成一个下拉列表,例如,如果我选择国家/地区,则将自动选择国家/地区代码,我已经尝试过并对其进行过处理,但是似乎没有完成?

I am trying to generate a dropdown list for signup page, like if i select the country the country code would be automatically selected i have already tried and worked on it but it did''nt seems to be done ?

推荐答案

如果这是ISO国家/地区代码,请参见:
http://en.wikipedia.org/wiki/Country_code [ http://countrycode.org/ [ ^ ].

您可以在本文引用的字母列表中找到每个国家的国家/地区代码信息.

为了方便起见,我首先将国家列表定义为枚举类型:
If this is ISO country code, please see:
http://en.wikipedia.org/wiki/Country_code[^].

This is a less complete list: http://countrycode.org/[^].

You can find country code information for each country in alphabetical lists referenced in this article.

For convenience, I would first defined a list of countries as a enumeration type:
enum Country = {
    Afghanistan = 4, 
    Åland_Islands = 248,
    Albania = 8,
    Algeria = 12,
    //...
}



此处的整数值为ISO 3166-1值:
http://en.wikipedia.org/wiki/ISO_3166-1 [ http://en.wikipedia.org/wiki/ISO_3166 [​​枚举类型不枚举!解决.NET和语言限制 [ ^ ].

有一些问题.如果您需要在每个国家/地区携带更多信息,则需要在一些内部数据收集中找到它.最方便的方法是只让枚举成员对数据数组进行索引.但是该怎么做呢?您将在上面引用的同一篇文章中找到答案.

另一个问题是:某些国家/地区名称不是有效的.NET标识符,主要是因为它们包含空格.例如,我在上面使用下划线写了ÅlandIslands".这对UI不利.当然,您可以在用空白填充UI之前用下划线替换下划线,但这是一种la脚的方法.我的另一篇文章介绍了这种激进的方法:
人类可读的枚举元数据 [ ^ ].

此方法不仅提供了与枚举成员相关的易于理解的名称,还提供了一种使国家名称全球化的方法,因此可以将UI本地化以在不同的文化中显示这些名称.

您也可以使用替代方法或其他方法.首先,在所有情况下,最好从存储在服务器上的某些数据文件中加载国家/地区信息.读取此文件时,会将数据存储在从数据填充的集合中.如果您需要存储一些与国家相关的信息,而不仅仅是国家名称和代码,这将特别有用.我建议使用System.Collections.Generic.Dictionary<key, value>.请参阅:
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx [ ^ ].

类型value是用于携带与每个国家/地区有关的所有所需信息的数据类,并且key类型应该是下拉列表中一个国家/地区的索引,因此可以使用System.Int32System.UInt32.这样,您可以通过列表中的选择位置轻松找到所需的记录.您可能需要通过其他键(例如国家/地区代码)为该国家/地区信息编制索引.
确保您正确创建了两个或更多相关字典.首先,确保您的value类型是引用,而不是值类型(上面,我说"class"而不是"structure"是引用类型).确保两个或更多词典共享相同的值.这样,您将只有一个对象代表从两个或多个不同集合中引用的国家/地区.

—SA



The integer values here are ISO 3166-1 values:
http://en.wikipedia.org/wiki/ISO_3166-1[^],
http://en.wikipedia.org/wiki/ISO_3166[^].

If this is all you need, this would be enough. You can populate your list control from the data found in this enumeration type. You can find how write the loop iterating enumeration values from my article:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^].

There are some problems. If you need to carry more information in each country, you would need to find it in some internal data collection. The most convenient way is to have just the array of data indexed by the enumeration member. But how to do it? You will find the answer in the same article referenced above.

Another problem is this: some country names are not valid .NET identifiers, mostly because they contain blank spaces. For example, I''ve written "Åland Islands" above using underscore. This is not good for the UI. Of course, you can replace underscores before population of the UI with blank spaces, but this is a lame approach. The radical approach is shown in my other article:
Human-readable Enumeration Meta-data[^].

Not only this method provides for human-readable names associated with enumeration members, it also provides a way to globalize the names of the country, so the UI can be localized to show these names in different cultures.

You can also use alternative or additional approach. First of all, in all cases it''s good to load country information from some data file you store on the server. When you read this file, you store the data in a collection you populate from data. This is especially useful if you need to store some country-related information, not just country name and code, which is very likely. I would suggest to use System.Collections.Generic.Dictionary<key, value>. Please see:
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^].

The type value would be the data class used to carry all information you need relevant to each country, and the key type should be the index of a country in a dropdown list, so you can use System.Int32, or System.UInt32. This way, you can easily find required record by the position of selection in the list. You may need to index this country information by another key, such as country code.
Make sure, that you create two or more related dictionaries correctly. First of all, make sure your value type is a reference, not a value type (above, I said "class", not "structure", to have a reference type). Make sure the two or more dictionaries share the same values. These way, you will have only one object representing a country referenced from two or more different collections.

—SA


这篇关于国家/地区代码的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆