private string LookupCountry() { string IP; bool IsLocal = false; bool _CacheGeoIPData = true; string _GeoIPFile; _GeoIPFile = "controls/CountryListBox/Data/GeoIP.dat"; if (Page.Request.UserHostAddress == "127.0.0.1") { //'The country cannot be detected because the user is local. IsLocal = true; //Set the IP address in case they didn't specify LocalhostCountryCode IP = Page.Request.UserHostAddress; } else { //Set the IP address so we can find the country IP = Page.Request.UserHostAddress; } //Check to see if we need to generate the Cache for the GeoIPData file if (Context.Cache.Get("GeoIPData") == null && _CacheGeoIPData) { //Store it as well as setting a dependency on the file Context.Cache.Insert("GeoIPData", CountryLookup.FileToMemory(Context.Server.MapPath(_GeoIPFile)), new CacheDependency(Context.Server.MapPath(_GeoIPFile))); } //Check to see if the request is a localhost request //and see if the LocalhostCountryCode is specified if (IsLocal) { return Null.NullString; } //Either this is a remote request or it is a local //request with no LocalhostCountryCode specified CountryLookup _CountryLookup; //Check to see if we are using the Cached //version of the GeoIPData file if (_CacheGeoIPData) { //Yes, get it from cache _CountryLookup = new CountryLookup((MemoryStream) Context.Cache.Get("GeoIPData")); } else { //No, get it from file _CountryLookup = new CountryLookup(Context.Server.MapPath(_GeoIPFile)); } //Get the country code based on the IP address string country = Null.NullString; try { country = _CountryLookup.LookupCountryName(IP); } catch (Exception ex) { Exceptions.LogException(ex); } return country; }
protected override void OnDataBinding( EventArgs e ) { bool isLocal = false; if( ! Page.IsPostBack ) { //If GeoIPFile is not provided, assume they put it in BIN. if( String.IsNullOrEmpty( _GeoIPFile ) ) { _GeoIPFile = "controls/CountryListBox/Data/GeoIP.dat"; } EnsureChildControls(); //Check to see if a TestIP is specified string ipAddress; if( !String.IsNullOrEmpty(_TestIP) ) { //TestIP is specified, let's use it ipAddress = _TestIP; } else if( this.Page.Request.UserHostAddress == "127.0.0.1" ) { //The country cannot be detected because the user is local. isLocal = true; //Set the IP address in case they didn't specify LocalhostCountryCode ipAddress = this.Page.Request.UserHostAddress; } else { //Set the IP address so we can find the country ipAddress = this.Page.Request.UserHostAddress; } //Check to see if we need to generate the Cache for the GeoIPData file if( Context.Cache.Get( "GeoIPData" ) == null && _CacheGeoIPData ) { //Store it as well as setting a dependency on the file Context.Cache.Insert( "GeoIPData", CountryLookup.FileToMemory( Context.Server.MapPath( _GeoIPFile ) ), new CacheDependency( Context.Server.MapPath( _GeoIPFile ) ) ); } //Check to see if the request is a localhost request //and see if the LocalhostCountryCode is specified if( isLocal && !String.IsNullOrEmpty(_LocalhostCountryCode) ) { //Bing the data base.OnDataBinding( e ); //Pre-Select the value in the drop-down based //on the LocalhostCountryCode specified. if( !Convert.ToBoolean( this.Items.FindByValue( _LocalhostCountryCode ) == null ) ) { this.Items.FindByValue( _LocalhostCountryCode ).Selected = true; } } else { //Either this is a remote request or it is a local //request with no LocalhostCountryCode specified CountryLookup _CountryLookup; //Check to see if we are using the Cached //version of the GeoIPData file if( _CacheGeoIPData ) { //Yes, get it from cache _CountryLookup = new CountryLookup( (MemoryStream)Context.Cache.Get( "GeoIPData" ) ); } else { //No, get it from file _CountryLookup = new CountryLookup( Context.Server.MapPath( _GeoIPFile ) ); } //Get the country code based on the IP address string _UserCountryCode = _CountryLookup.LookupCountryCode( ipAddress ); //Bind the datasource base.OnDataBinding( e ); //Make sure the value returned is actually //in the drop-down list. if( !Convert.ToBoolean( this.Items.FindByValue( _UserCountryCode ) == null ) ) { //Yes, it's there, select it based on its value this.Items.FindByValue( _UserCountryCode ).Selected = true; } else { //No it's not there. Let's get the Country description //and add a new list item for the Country detected string _UserCountry = _CountryLookup.LookupCountryName( ipAddress ); if( _UserCountry != "N/A" ) { ListItem newItem = new ListItem(); newItem.Value = _UserCountryCode; newItem.Text = _UserCountry; this.Items.Insert( 0, newItem ); //Now let's Pre-Select it this.Items.FindByValue( _UserCountryCode ).Selected = true; } } } } }