2 // System.Web.UI.WebControls.AdRotator
5 // Ben Maurer <bmaurer@novell.com>
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
31 using System.ComponentModel;
33 namespace System.Web.UI.WebControls {
34 [DefaultEvent("AdCreated")]
35 [DefaultProperty("AdvertisementFile")]
36 [Designer("System.Web.UI.Design.WebControls.AdRotatorDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
38 [ToolboxData("<{0}:AdRotator runat=\"server\"></{0}:AdRotator>")]
40 [ToolboxData("<{0}:AdRotator runat=\"server\" Height=\"60px\" Width=\"468px\"></{0}:AdRotator>")]
42 public class AdRotator :
51 protected internal override void OnInit (EventArgs e)
62 override void OnPreRender (EventArgs eee)
66 if (ad_file != "" && ad_file != null) {
67 ReadAdsFromFile (Page.MapPath (ad_file));
71 AdCreatedEventArgs e = new AdCreatedEventArgs (ht);
79 protected internal override void PerformDataBinding (IEnumerable data)
81 throw new NotImplementedException ();
85 protected override void PerformSelect ()
87 throw new NotImplementedException ();
91 AdCreatedEventArgs createdargs;
98 override void Render (HtmlTextWriter w)
100 AdCreatedEventArgs e = createdargs;
102 base.AddAttributesToRender (w);
104 if (e.NavigateUrl != null)
105 w.AddAttribute (HtmlTextWriterAttribute.Href, ResolveUrl (e.NavigateUrl));
106 w.AddAttribute (HtmlTextWriterAttribute.Target, Target);
108 w.RenderBeginTag (HtmlTextWriterTag.A);
110 if (e.NavigateUrl != null)
111 w.AddAttribute (HtmlTextWriterAttribute.Src, ResolveUrl (e.ImageUrl));
113 w.AddAttribute (HtmlTextWriterAttribute.Alt, e.AlternateText == null ? "" : e.AlternateText);
114 w.AddAttribute (HtmlTextWriterAttribute.Border, "0");
115 w.RenderBeginTag (HtmlTextWriterTag.Img);
116 w.RenderEndTag (); // img
117 w.RenderEndTag (); // a
121 // We take all the ads in the ad file and add up their
122 // impression weight. We then take a random number [0,
123 // TotalImpressions). We then choose the ad that is
124 // that number or less impressions for the beginning
125 // of the file. This lets us respect the weights
128 Hashtable ChooseAd ()
130 // cache for performance
131 string KeywordFilter = this.KeywordFilter;
136 foreach (Hashtable a in ads) {
137 if (KeywordFilter == "" || KeywordFilter == (string) a ["Keyword"])
138 total_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
141 int r = new Random ().Next (total_imp);
143 foreach (Hashtable a in ads) {
144 if (KeywordFilter != "" && KeywordFilter != (string) a ["Keyword"])
146 cur_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
153 throw new Exception ("I should only get here if no ads matched");
158 ArrayList ads = new ArrayList ();
160 void ReadAdsFromFile (string s)
162 XmlDocument d = new XmlDocument ();
165 } catch (Exception e) {
166 throw new HttpException ("AdRotator could not parse the xml file", e);
171 foreach (XmlNode n in d.DocumentElement.ChildNodes) {
173 Hashtable ad = new Hashtable ();
175 foreach (XmlNode nn in n.ChildNodes)
176 ad.Add (nn.Name, nn.InnerText);
189 [Editor("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
190 [WebSysDescription("")]
191 [WebCategory("Behavior")]
192 public string AdvertisementFile {
203 [DefaultValue ("AlternateText")]
204 [WebSysDescriptionAttribute ("")]
205 [WebCategoryAttribute ("Behavior")]
207 public string AlternateTextField
210 throw new NotImplementedException ();
213 throw new NotImplementedException ();
219 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
220 [EditorBrowsable(EditorBrowsableState.Never)]
221 public override FontInfo Font {
228 [DefaultValue ("ImageUrl")]
230 [WebSysDescriptionAttribute ("")]
231 [WebCategoryAttribute ("Behavior")]
232 public string ImageUrlField
235 throw new NotImplementedException ();
238 throw new NotImplementedException ();
246 [WebSysDescription("")]
247 [WebCategory("Behavior")]
248 public string KeywordFilter {
250 return ViewState.GetString ("KeywordFilter", "");
253 ViewState ["KeywordFilter"] = value;
259 [DefaultValue ("NavigateUrl")]
261 [WebSysDescriptionAttribute ("")]
262 [WebCategoryAttribute ("Behavior")]
263 public string NavigateUrlField
266 throw new NotImplementedException ();
269 throw new NotImplementedException ();
273 /* listed in corcompare */
275 public override Page Page
287 [DefaultValue("_top")]
288 [TypeConverter(typeof(System.Web.UI.WebControls.TargetConverter))]
289 [WebSysDescription("")]
290 [WebCategory("Behavior")]
291 public string Target {
293 return ViewState.GetString ("Target", "_top");
296 ViewState ["Target"] = value;
302 /* all these are listed in corcompare */
304 public override string UniqueID
307 return base.UniqueID;
312 protected override HtmlTextWriterTag TagKey
320 protected override StateBag ViewState
323 return base.ViewState;
328 protected virtual void OnAdCreated (AdCreatedEventArgs e)
330 AdCreatedEventHandler h = (AdCreatedEventHandler) Events [AdCreatedEvent];
335 static readonly object AdCreatedEvent = new object ();
337 [WebSysDescription("")]
338 [WebCategory("Action")]
339 public event AdCreatedEventHandler AdCreated {
340 add { Events.AddHandler (AdCreatedEvent, value); }
341 remove { Events.RemoveHandler (AdCreatedEvent, value); }