封装一个控件,首先当然要知道呈现的HTML代码了,其中Banner要判断是否为Flash或是图片,如果是Flash时我们还应该加入他所特有的一些属性,这个控件继承自System.Web.UI.WebControls.WebControl类,重写RenderContent方法实现,其他的一些说明在代码中有说明,这里就不多说了。下面我们来看看代码:
1using System;
2using System.IO;
3using System.Text;
4using System.Web.UI;
5using System.ComponentModel;
6using System.Web.UI.WebControls;
7
8namespace NExplus.Controls
9{
10 /// <summary>
11 /// 横幅广告控件。
12 /// </summary>
13 public class Banner:WebControl
14 {
15 /// <summary>
16 /// 初始化<see cref="Banner"/>类。
17 /// </summary>
18 public Banner() { }
19 /// <summary>
20 /// 获取或设置图片或Flash的路径。
21 /// </summary>
22 [DefaultValue(""),
23 MergableProperty(false),
24 Description("The path of the banner!")]
25 public string Path {
26 get {
27 object obj = this.ViewState["BannerPath"];
28 if (obj != null)
29 return (String)obj;
30 return null;
31 }
32 set {
33 this.ViewState["BannerPath"] = value;
34 }
35 }
36 /// <summary>
37 /// 获取或设置Flash的品质,如果是图片这将失效。
38 /// </summary>
39 [DefaultValue("high"), MergableProperty(false)]
40 public string FlashQuality
41 {
42 get
43 {
44 object obj = this.ViewState["FlashQuality"];
45 if (obj != null)
46 return Helper.IsNullOrEmpty((String)obj) ? "high" : (String)obj;
47 return "high";
48 }
49 set
50 {
51 this.ViewState["FlashQuality"] = value;
52 }
53 }
54 /// <summary>
55 /// 获取或设置是否为Flash,默认为<c>false</c>。
56 /// </summary>
57 [DefaultValue(false),
58 MergableProperty(false)]
59 public bool IsFlash {
60 get {
61 object obj = this.ViewState["IsFlash"];
62 if (obj != null)
63 return (bool)obj;
64 return false;
65 }
66 set {
67 this.ViewState["IsFlash"] = value;
68 }
69 }
70 /// <summary>
71 /// 获取或设置是否为透明广告。
72 /// </summary>
73 [DefaultValue(false),
74 MergableProperty(false)]
75 public bool IsFlashTransparent
76 {
77 get
78 {
79 object obj = this.ViewState["IsFlashTransparent"];
80 if (obj != null)
81 return (bool)obj;
82 return false;
83 }
84 set
85 {
86 this.ViewState["IsFlashTransparent"] = value;
87 }
88 }
89 /// <summary>
90 /// 重写并呈现控件。
91 /// </summary>
92 /// <param name="writer"><see cref="HtmlTextWriter"/>对象。</param>
93 protected override void RenderContents(HtmlTextWriter writer)
94 {
95 if (this.IsFlash && !Helper.IsNullOrEmpty(this.Path))
96 {
97 writer.Write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"{0}\" height=\"{1}\" id=\"movie\" align=\"\">{2}", this.Width == Unit.Empty ? 480 : this.Width, this.Height==Unit.Empty?60:this.Height, Environment.NewLine);
98 writer.Write("<param name=\"movie\" value=\"{0}\">{1}", this.Path, Environment.NewLine);
99 if (!Helper.IsNullOrEmpty(this.FlashQuality))
100 writer.Write("<param name=\"quality\" value=\"{0}\">{1}", this.FlashQuality, Environment.NewLine);
101 if (this.IsFlashTransparent)
102 writer.Write("<param name=\"wmode\" value=\"transparent\">{0}", Environment.NewLine);
103 writer.Write("<embed src=\"{0}\" quality=\"{1}\" width=\"{2}\" height=\"{3}\" name=\"movie\" align=\"\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\">{4}", this.Path,this.FlashQuality, this.Width == Unit.Empty ? 480 : this.Width, this.Height == Unit.Empty ? 60 : this.Height, Environment.NewLine);
104 writer.Write("</object>");
105 }
106 else if (!Helper.IsNullOrEmpty(this.Path))
107 {
108 writer.Write("<img src=\"{0}\" width=\"{1}\" height=\"{2}\" alt=\"{3}\" border=\"0\"/>", this.Path, this.Width == Unit.Empty ? 480 : this.Width, this.Height == Unit.Empty ? 60 : this.Height, this.ToolTip);
109 }
110 base.RenderContents(writer);
111 }
112 }
113}
114
2using System.IO;
3using System.Text;
4using System.Web.UI;
5using System.ComponentModel;
6using System.Web.UI.WebControls;
7
8namespace NExplus.Controls
9{
10 /// <summary>
11 /// 横幅广告控件。
12 /// </summary>
13 public class Banner:WebControl
14 {
15 /// <summary>
16 /// 初始化<see cref="Banner"/>类。
17 /// </summary>
18 public Banner() { }
19 /// <summary>
20 /// 获取或设置图片或Flash的路径。
21 /// </summary>
22 [DefaultValue(""),
23 MergableProperty(false),
24 Description("The path of the banner!")]
25 public string Path {
26 get {
27 object obj = this.ViewState["BannerPath"];
28 if (obj != null)
29 return (String)obj;
30 return null;
31 }
32 set {
33 this.ViewState["BannerPath"] = value;
34 }
35 }
36 /// <summary>
37 /// 获取或设置Flash的品质,如果是图片这将失效。
38 /// </summary>
39 [DefaultValue("high"), MergableProperty(false)]
40 public string FlashQuality
41 {
42 get
43 {
44 object obj = this.ViewState["FlashQuality"];
45 if (obj != null)
46 return Helper.IsNullOrEmpty((String)obj) ? "high" : (String)obj;
47 return "high";
48 }
49 set
50 {
51 this.ViewState["FlashQuality"] = value;
52 }
53 }
54 /// <summary>
55 /// 获取或设置是否为Flash,默认为<c>false</c>。
56 /// </summary>
57 [DefaultValue(false),
58 MergableProperty(false)]
59 public bool IsFlash {
60 get {
61 object obj = this.ViewState["IsFlash"];
62 if (obj != null)
63 return (bool)obj;
64 return false;
65 }
66 set {
67 this.ViewState["IsFlash"] = value;
68 }
69 }
70 /// <summary>
71 /// 获取或设置是否为透明广告。
72 /// </summary>
73 [DefaultValue(false),
74 MergableProperty(false)]
75 public bool IsFlashTransparent
76 {
77 get
78 {
79 object obj = this.ViewState["IsFlashTransparent"];
80 if (obj != null)
81 return (bool)obj;
82 return false;
83 }
84 set
85 {
86 this.ViewState["IsFlashTransparent"] = value;
87 }
88 }
89 /// <summary>
90 /// 重写并呈现控件。
91 /// </summary>
92 /// <param name="writer"><see cref="HtmlTextWriter"/>对象。</param>
93 protected override void RenderContents(HtmlTextWriter writer)
94 {
95 if (this.IsFlash && !Helper.IsNullOrEmpty(this.Path))
96 {
97 writer.Write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"{0}\" height=\"{1}\" id=\"movie\" align=\"\">{2}", this.Width == Unit.Empty ? 480 : this.Width, this.Height==Unit.Empty?60:this.Height, Environment.NewLine);
98 writer.Write("<param name=\"movie\" value=\"{0}\">{1}", this.Path, Environment.NewLine);
99 if (!Helper.IsNullOrEmpty(this.FlashQuality))
100 writer.Write("<param name=\"quality\" value=\"{0}\">{1}", this.FlashQuality, Environment.NewLine);
101 if (this.IsFlashTransparent)
102 writer.Write("<param name=\"wmode\" value=\"transparent\">{0}", Environment.NewLine);
103 writer.Write("<embed src=\"{0}\" quality=\"{1}\" width=\"{2}\" height=\"{3}\" name=\"movie\" align=\"\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\">{4}", this.Path,this.FlashQuality, this.Width == Unit.Empty ? 480 : this.Width, this.Height == Unit.Empty ? 60 : this.Height, Environment.NewLine);
104 writer.Write("</object>");
105 }
106 else if (!Helper.IsNullOrEmpty(this.Path))
107 {
108 writer.Write("<img src=\"{0}\" width=\"{1}\" height=\"{2}\" alt=\"{3}\" border=\"0\"/>", this.Path, this.Width == Unit.Empty ? 480 : this.Width, this.Height == Unit.Empty ? 60 : this.Height, this.ToolTip);
109 }
110 base.RenderContents(writer);
111 }
112 }
113}
114
也许有些属性还没封装好,希望批评指教!