(一). 概述
通过制作一个<计算器>功能自定义复合控件演示复合控件的制作
(二). 代码
1. 核心控件实现代码文件computecontrol.cs
1using system;
2using system.collections.generic;
3using system.text;
4using system.web;
5using system.web.ui;
6using system.web.ui.webcontrols;
7
8namespace kingcontrols
9{
10 public class computecontrol : system.web.ui.webcontrols.compositecontrol
11 {
12 //操作数
13 private textbox tb1;
14 private textbox tb2;
15
16 //显示结果
17 private label lb;
18
19 //操作(+-*/)
20 private button bt1;
21 private button bt2;
22 private button bt3;
23 private button bt4;
24
25 protected override void createchildcontrols()
26 {
27 tb1 = new textbox();
28 tb1.id = "textbox1";
29 this.controls.add( tb1 );
30
31 tb2 = new textbox();
32 tb2.id = "textbox2";
33 this.controls.add(tb2);
34
35 lb = new label();
36 lb.id = "label1";
37 lb.backcolor = system.drawing.color.yellowgreen;
38 lb.text = "result: ";
39 this.controls.add( lb );
40
41 bt1 = new button();
42 bt1.id = "button1";
43 bt1.text = "+";
44 bt1.commandargument = "+";
45 bt1.click += new eventhandler(bt_click);
46 this.controls.add( bt1 );
47 bt2 = new button();
48 bt2.id = "button2";
49 bt2.text = "-";
50 bt2.commandargument = "-";
51 bt2.click += new eventhandler(bt_click);
52 this.controls.add(bt2);
53 bt3 = new button();
54 bt3.id = "button3";
55 bt3.text = "*";
56 bt3.commandargument = "*";
57 bt3.click += new eventhandler(bt_click);
58 this.controls.add(bt3);
59 bt4 = new button();
60 bt4.id = "button4";
61 bt4.text = "/";
62 bt4.commandargument = "/";
63 bt4.click += new eventhandler(bt_click);
64 this.controls.add(bt4);
65 }
66
67 void bt_click(object sender, eventargs e)
68 {
69 try
70 {
71 if ("result: " != lb.text)
72 {
73 lb.text = lb.text + ", ";
74 }
75 switch( ((button)sender).commandargument )
76 {
77 case "+": lb.text = lb.text + convert.tostring(convert.toint32(this.tb1.text) + convert.toint32(this.tb2.text)); break;
78 case "-": lb.text = lb.text + convert.tostring(convert.toint32(this.tb1.text) - convert.toint32(this.tb2.text)); break;
79 case "*": lb.text = lb.text + convert.tostring(convert.toint32(this.tb1.text) * convert.toint32(this.tb2.text)); break;
80 case "/": lb.text = lb.text + convert.tostring(convert.toint32(this.tb1.text) / convert.toint32(this.tb2.text)); break;
81 }
82 }
83 catch
84 {
85 lb.text = "it's is not right format, please input again.";
86 }
87 }
88
89 protected override void rendercontents(htmltextwriter output)
90 {
91 output.write("a");
92
93 }
94
95 protected override void render(htmltextwriter writer)
96 {
97 writer.renderbegintag(htmltextwritertag.table);
98
99 //operating item 1
100 writer.renderbegintag(htmltextwritertag.tr);
101 writer.renderbegintag(htmltextwritertag.td);
102 tb1.rendercontrol(writer);
103 writer.renderendtag();
104 writer.renderendtag();
105
106 //<br>
107 writer.writebreak();
108
109 //operating symbol
110 writer.renderbegintag(htmltextwritertag.tr);
111 writer.renderbegintag(htmltextwritertag.td);
112 writer.writeencodedtext(" + - * / ");
113 writer.renderendtag();
114 writer.renderendtag();
115
116 //operating item2
117 writer.renderbegintag(htmltextwritertag.tr);
118 writer.renderbegintag(htmltextwritertag.td);
119 tb2.rendercontrol(writer);
120 writer.renderendtag();
121 writer.renderendtag();
122
123 //operating symbol
124 writer.renderbegintag(htmltextwritertag.tr);
125 writer.renderbegintag(htmltextwritertag.td);
126 writer.writeencodedtext(" equal ");
127 writer.renderendtag();
128 writer.renderendtag();
129
130 //the relust label
131 writer.renderbegintag(htmltextwritertag.tr);
132 writer.renderbegintag(htmltextwritertag.td);
133 lb.rendercontrol(writer);
134 writer.renderendtag();
135 writer.renderendtag();
136
137 //button1
138 writer.renderbegintag(htmltextwritertag.tr);
139 writer.renderbegintag(htmltextwritertag.nobr);
140 writer.renderbegintag(htmltextwritertag.td);
141 bt1.rendercontrol(writer);
142 bt2.rendercontrol(writer);
143 bt3.rendercontrol(writer);
144 bt4.rendercontrol(writer);
145 writer.renderendtag();
146 writer.renderendtag();
147
148 writer.renderendtag();
149 }
150 }
151}
152
2. 前台页面代码 usingcompositecontrol.aspx (使用方法)
1<%@ page language="c#" autoeventwireup="true" codefile="usingcompositecontrol.aspx.cs" inherits="usingcompositecontrol" %>
2<%@ register assembly="kingcontrols" namespace="kingcontrols" tagprefix="ksp" %>
3… …
4<ksp:computecontrol id="compute1" runat="server" />
5… …
6
(三). 示例代码下载
http://www.cnblogs.com/files/chengking/kingcontrols.rar
新闻热点
疑难解答
图片精选