Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 ?
Posted
Comments
Sergey Alexandrovich Kryukov 1-Apr-12 11:54am    
Is it ISO country code: http://en.wikipedia.org/wiki/Country_code?
--SA
Sergey Alexandrovich Kryukov 1-Apr-12 12:27pm    
[onurag commented (moved from "Solution"):]

We Are Here To Help U Not to Provide the Code Please Put The code that u have written & Sure we can can help u out.
Thanks...
[no name] 1-Apr-12 14:08pm    
Sorry for posting that in answer

1 solution

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:
C#
enum Country = {
    Afghanistan = 4, 
    Åland_Islands = 248,
    Albania = 8,
    Algeria = 12,
    //...
}


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
 
Share this answer
 
v2
Comments
Dean Oliver 1-Apr-12 14:56pm    
excellent gave you 5! But where do you get the time to do all this on a sunday? Keep up the great work.
Sergey Alexandrovich Kryukov 1-Apr-12 15:06pm    
Thank you Dean.
Between having a long sleep after the travel to the horse riding barn and before visiting the farm to enjoy some nature and meeting with farm animals we visit on regular basis... You cannot say my weekend it too miserable... :-)
--SA
sallaudin 1-Apr-12 15:01pm    
i have added my view for signup page i need to dynamically add this country list in my view "@{
ViewBag.Title = "CountriesList";
}
<option value="UnitedStates">UnitedStates</option>
<option value="UnitedKingdom">UnitedKingdom</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>"


like if i select united states the signup page dynamically shows +1 ..
Sergey Alexandrovich Kryukov 1-Apr-12 15:34pm    
Don't do it in HTML. Did anyone teach you to separate code, data and presentation layer? You are trying to do it in HTML. Don't do it. Create some XML file or even a text file. On server side, write code reading the file and populating the UI. If you don't know how to do this, ask another question.
--SA
sallaudin 1-Apr-12 16:41pm    
actually i am trying to do this via MVC3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900