Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anthony Candaele 1197 posts 2049 karma points
    Jan 05, 2012 @ 19:40
    Anthony Candaele
    0

    how to access uComponents Country Picker from usercontrol

    Hi,

    For my memberRegister.ascx usercontrol I need to access the values from a uComponents Country Picker datatype:

    To populate a dropdownlist with the countries from the uComponents Country Pickerk, I use this code in my user control:

     

    DropDownList ddlCountry = (DropDownList)cuw.CreateUserStep.ContentTemplateContainer.FindControl("Country");

                XPathNodeIterator pvCountry = umbraco.library.GetPreValues(1359);

                pvCountry.MoveNext();

                XPathNodeIterator pvCountryValues = pvCountry.Current.SelectChildren("preValue", "");

                while (pvCountryValues.MoveNext())

                {

                    ddlCountry.Items.Add(new ListItem(pvCountryValues.Current.Value, pvCountryValues.Current.GetAttribute("id", "")));

                }

    Where umbraco.library.GetPreValues(1359) is the hardcoded id from my uComponents Country Picker

    However this is the result in my registration page:

    Not exactly the result I was looking for :)

    So how can I populate a dropdownlist control in my memberRegister.ascx from my uComponents Country Picker datatype?

    Or is there another solution to this problem?

    Thanks for your help,

    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 05, 2012 @ 20:54
    Anthony Candaele
    0

    Maybe I asked my question the wrong way, the point is, I want a dropdownlist with countries in my registration form.

    How can I implement a dropdownlist with countries in my memberRegister.ascx user control without manualy typing all the countries?

    Thanks for your help,

    Anthony

  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Jan 05, 2012 @ 21:08
    Lee Kelleher
    2

    Hi Anthony,

    The uComponents Country Picker populates the list from .NET's System.Globalization namespace/methods.  I'd feel it to be overkill trying to get the values from the Country Picker data-type.  Instead, use the same code as we do.

    Simon Dingley has a blog post with a code snippet: http://www.prolificnotion.co.uk/c-utility-method-to-populate-list-controls-with-all-countries-as-given-in-iso-3166-1/

    Cheers, Lee.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 05, 2012 @ 21:08
    Tom Fulton
    2

    Hi Anthony,

    If you're OK with the fact that the uComponents Country Picker is missing some countries (I think you said you were), you could use the same method to populate your dropdown.  Check out the source code for the CountryPicker - check the FillWithIsoCountries method - you can just copy that into your usercontrol.  See this blog for more info on how it works.

    Otherwise as we talked about previously you can populate a table in your DB with a country list and read from that

    Hope this helps,
    Tom

  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Jan 05, 2012 @ 21:09
    Lee Kelleher
    0

    Ah, cross-posted at the same time! ;-)  Great minds, eh?!

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 05, 2012 @ 21:10
    Tom Fulton
    0

    Haha, indeed!

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 06, 2012 @ 15:07
    Anthony Candaele
    0

    Hi Lee, hi Tom,

    I used the solution as suggested here:

    http://www.prolificnotion.co.uk/c-utility-method-to-populate-list-controls-with-all-countries-as-given-in-iso-3166-1/

    I created a Utilities.cs class and just pasted the code in, than I filled my dropdowlist control like this:

    DropDownList ddlCountry = (DropDownList)cuw.CreateUserStep.ContentTemplateContainer.FindControl("Country");
    Utilities.FillWithISOCountries(ddlCountry);

    And it works like a charm

    Thanks a lot for your help,

    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 06, 2012 @ 15:44
    Anthony Candaele
    0

    Hi Guys,

    I'm back already. The problem I'm facing now is to write back a selected country from my countrie dropdownlist in the registration form to the member profile:

    for that purpose I use the code I found in Mike Taylors blog on creating a usercontrol for a registration form (an excellent blog post by the way)

    I used for instance this code to save checked values from a checkbox list to the jobseeker membertype:

    CheckBoxList chkList = (CheckBoxList)cuw.CreateUserStep.ContentTemplateContainer.FindControl("FieldsExperience");

                    string chkboxValue = "";

                    foreach (ListItem item in chkList.Items)

                        if (item.Selected)

                            chkboxValue += (chkboxValue == "" ? "" : ",") + item.Value;

                    mp.JobExperience = chkboxValue;

    I tried to do the same with the Countries dropdownlist:

    DropDownList ddlCountry = (DropDownList)cuw.CreateUserStep.ContentTemplateContainer.FindControl("Country");

                    string ddlCountryValue = "";

                    foreach (ListItem item in ddlCountry.Items)

                   

                        if (item.Selected)

                            ddlCountryValue += (ddlCountryValue == "" ? "" : ",") + item.Value;

                    mp.Country = ddlCountryValue;

    But this isn't working, after submitting the registration form, the selected country is not saved to my member_country property of the jobseeker member type (this property is of datatype uComponents Country Picker:

    So my question is, how can I save a selected country in the registration form to my membertype?

    Thanks for your help,

    Anthony

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 06, 2012 @ 16:13
    Tom Fulton
    0

    Hi Anthony,

    It looks like the Country Picker stores the full country name for it's data, but the example on the blog stores the code (ie US instead of United States).  To fix just adjust your code to grab the Text instead of the Value.

    Also, no need to do a loop, that's needed for the CheckBoxList since it can have multiple items selected, but for the dropdown you can just do:

    string ddlCountryValue = ddlCountry.SelectedItem.Text;
    mp.Country = ddlCountryValue;

    Hope this helps,
    Tom

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 06, 2012 @ 16:44
    Anthony Candaele
    0

    Hi Tom,

    Yes, the selected country is saved now to the created member:

    But I'll do some more testing on this because on my first test, I chose "Panama", and the country that was saved to the created member was "Belgium"!

    Maybe there are some inconsistencies between the values that are created by the .NET utitlity class and the uComponents Country Picker, although Lee said that the uComponents Country Picker is also using this utility class for it's list of countries.

    Anyway as always, thanks a lot for your help, and have a nice weekend,

    Anthony

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 06, 2012 @ 16:47
    Tom Fulton
    1

    Hi Anthony,

    You are in Belgium aren't you?  I think the code on the blog attempts to set the selected country to your country (mine shows US by default).  So perhaps there's some sort of viewstate/lifecycle issue.  Make sure that you only populate the dropdown on the initial load and not again on a postback

    if (!Page.IsPostBack) {
      Utilities.FillWithISOCountries(ddlCountry);
    }

    -Tom

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 06, 2012 @ 18:07
    Anthony Candaele
    0

    yup, it's working now. I created a test user and selected Panama and the country is saved correctly to the created member:

    :)

    And yes, I'm from Belgium.

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 29, 2012 @ 19:48
    Anthony Candaele
    0

    Hi, Tom, Lee,

    I'm working now on a user control that lists all members from a given Member Group ("jobseekers", "employers").

    I access a member profile using the MemberProfile.GetUserProfile() method

    however, when I access the member's country property (of datatype uComponents Country Picker) I just get a number.

    So my question is, how can I get to the Country text string that is associated with this number?

    Thanks for your help,

    Anthony

     

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 30, 2012 @ 12:47
    Anthony Candaele
    0

    Hi Tom, Lee,

    Problem solved. Apparently I was reading out old testmembers where the country value wasn't set.

    greetings,

    Anthony

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies