File: D:/HostingSpaces/TDijk1/erp-apps.eu/wwwroot/CMSFormControls/Pux/PuxCountrySelector.ascx.cs
using CMS.FormControls;
using CMS.Globalization;
using CMS.Helpers;
using Pux.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class CMSFormControls_Pux_PuxCountrySelector : CountryFormSelector
{
List<string> _previousConditions = null;
public override object Value
{
get
{
return drpCountries.SelectedValue;
}
set
{
SetValue("selectedCountryID",ValidationHelper.GetString(value, string.Empty));
string stringValue = ValidationHelper.GetString(value, string.Empty);
if (drpCountries.Items.FindByValue(stringValue) != null)
{
drpCountries.SelectedValue = stringValue;
}
}
}
protected class CountryDropDownListItem
{
public string CountryDisplayName { get; set; }
public int CountryID { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (drpCountries.Items.Count == 0)
{
ReloadData();
}
}
public override void ReloadData()
{
if (_previousConditions != AllowedCountriesList || (AllowedCountriesList == null && drpCountries.Items.Count == 0))
{
var countries = CountryInfoProvider.GetCountries();
if (AllowedCountriesList != null && AllowedCountriesList.Count > 0)
{
countries = countries.WhereIn("CountryTwoLetterCode", AllowedCountriesList);
}
List<CountryDropDownListItem> result = new List<CountryDropDownListItem>();
foreach (var countryInfo in countries)
{
string localizedProjectCountryName = ResHelper.GetString(CurrentSite.SiteName + ".Countries." + countryInfo.CountryName);
var localizedCountryName = ResHelper.GetString("Pux.Countries." + countryInfo.CountryName);
var countryDisplayName = (!localizedProjectCountryName.StartsWith(CurrentSite.SiteName + ".Countries")) ? localizedProjectCountryName : localizedCountryName.StartsWith("Pux.Countries") ? countryInfo.CountryDisplayName : localizedCountryName;
result.Add(new CountryDropDownListItem() { CountryDisplayName = countryDisplayName, CountryID = countryInfo.CountryID });
}
if (!String.IsNullOrEmpty(DefaultValueText))
{
result.Insert(0, new CountryDropDownListItem() { CountryDisplayName = ResHelper.LocalizeString(DefaultValueText), CountryID = 0 });
}
drpCountries.DataSource = result;
drpCountries.DataBind();
var selectedValue = ValidationHelper.GetString( GetValue("selectedCountryID"),"");
if (!string.IsNullOrEmpty(selectedValue) && drpCountries.Items.FindByValue(selectedValue) != null)
{
drpCountries.SelectedValue = selectedValue;
}
if (DefaultCountryID != 0 && (drpCountries.SelectedIndex < 0 || drpCountries.SelectedValue == "0"))
{
if (drpCountries.Items.FindByValue(DefaultCountryID.ToString()) != null)
{
drpCountries.SelectedValue = DefaultCountryID.ToString();
}
}
_previousConditions = AllowedCountriesList;
}
}
}