asp.net|控制項|樣本|下載
(一). 概述
通過製作一個<計算機>功能自訂複合控制項示範複合控制項的製作
(二). 代碼
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