日期:2011-09-19  浏览次数:20493 次

(一). 概述
   通过制作一个<计算器>功能自定义复合控件演示复合控件的制作
(二). 代码
   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);