经常用到的Banner服务器控件的开发
无论是论坛,还是博客就连普通的个人网页就离不开Banner,Banner主要就是图片或Flash。以前经常用到都没有把他封装成一个服务器控件,今天有点空闲所以就把它封装了。
封装一个控件,首先当然要知道呈现的HTML代码了,其中Banner要判断是否为Flash或是图片,如果是Flash时我们还应该加入他所特有的一些属性,这个控件继承自System.Web.UI.WebControls.WebControl类,重写RenderContent方法实现,其他的一些说明在代码中有说明,这里就不多说了。下面我们来看看代码:
也许有些属性还没封装好,希望批评指教!
封装一个控件,首先当然要知道呈现的HTML代码了,其中Banner要判断是否为Flash或是图片,如果是Flash时我们还应该加入他所特有的一些属性,这个控件继承自System.Web.UI.WebControls.WebControl类,重写RenderContent方法实现,其他的一些说明在代码中有说明,这里就不多说了。下面我们来看看代码:
1
using System;
2
using System.IO;
3
using System.Text;
4
using System.Web.UI;
5
using System.ComponentModel;
6
using System.Web.UI.WebControls;
7
8
namespace 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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

也许有些属性还没封装好,希望批评指教!
分类:
ASP.NET
, ServerContols
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架