// sets vertical ad private void SetAdVertical(Ad_S2 _ad) { // get image name string _imagePath = GetImagePath(_ad.ImageName); // get control for image Control _leftBanner = FindControl("bannerLeft"); if (_leftBanner == null) { Trace.Warn("Shop", "Failed to find left ad banner"); return; } HyperLink _image = (HyperLink)FindControl("bannerLeftImg"); if (_image == null) { // create image _image = new HyperLink(); _image.ID = "bannerLeftImg"; // add to bannerTop _leftBanner.Controls.Add(_image); } // setup image _image.ImageUrl = _imagePath; _image.NavigateUrl = string.Format("http://{0}", _ad.Link); }
// picks random ad from given list for given type public static Ad_S2 GetAdd(List <Ad_S2> _ads, AddType _type) { bool _contains = false; // check that list contains type foreach (Ad_S2 _ad in _ads) { if (_ad.Type == _type) { _contains = true; break; } } if (!_contains) { throw new ArgumentException(string.Format("List of ads does not contain given type \"{0}\"", _type.ToString())); } // pick a random ad while (true) { Random _rand = new Random(); int _index = _rand.Next(_ads.Count); Ad_S2 _ad = _ads[_index]; if (_ad.Type == _type) { return(_ad); } } }
// loads advertisement banners private void loadAds() { // get list of adds List <Ad_S2> _ads; try { _ads = GetAds(); } catch (Exception _e) { Trace.Warn("Shop", "Failed to get ads", _e); return; } // get random vertical ads try { Ad_S2 _adV = Ad_S2.GetAdd(_ads, Ad_S2.AddType.Vertical); SetAdVertical(_adV); } catch (Exception _e) { Trace.Warn("Shop", "Failed to set vertical ad", _e); } }