自定义复合的控件,如果Winform中的控件一样
1:需要单独为其建一个项目,生成的文件以.dll为后缀名
2:使用的时候,添加引用,或者在工具箱中添加
3:如果自己手动添加则:在HTML头添加注册

<%@ Register TagPrefix="Custom" NameSpace="webcustomercontrol1" Assembly="webcustomercontrol1"%>

NameSpace 是控件的命名空间,Assembley是生成的程序集的名称,就是dll文件名.
4在form中<custom 类名 runat="server"/>
下面举几个例子详细说明:

  1. 第一个例子的效果图是这样:
    点击加的按钮时计算文本框中数字和,数字与数字之间以回车分隔
      1using System;
      2using System.Web.UI;
      3using System.Web.UI.WebControls;
      4using System.ComponentModel;
      5
      6namespace csMathControl
      7{
      8    /// <summary>
      9    /// WebCustomControl1 的摘要说明。
     10    /// </summary>

     11    [DefaultProperty("Text"),
     12        ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
     13    [DefaultEvent("Click")]
     14    public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
     15    {
     16        [Bindable(true),
     17        Category("Appearance"),
     18        DefaultValue("")]
     19    
     20        TextBox txtMath=new TextBox();
     21        Button butSum=new Button();
     22        Label lblResult=new Label();
     23        protected override void CreateChildControls()
     24        {
     25            //在该复合控件上添加SUB控件
     26            //设置TextMode属性和添加文本筐
     27            txtMath.TextMode=TextBoxMode.MultiLine;
     28            Controls.Add(txtMath);
     29            //开始新行
     30            Controls.Add(new LiteralControl("<br>"));
     31            //设置Text属性并且添加Button控件
     32            butSum.Text="   加   ";
     33            Controls.Add(butSum);
     34            //添加lable和literal控件来显示结果
     35            Controls.Add(new LiteralControl("&nbsp&nbsp结果:&nbsp<b>"));
     36            Controls.Add(lblResult);
     37            Controls.Add(new LiteralControl("<br>"));
     38            //开始处理事件
     39            butSum.Click+=new EventHandler(butSum_Click);
     40            
     41        }

     42
     43
     44
     45
     46
     47        /// <summary> 
     48        /// 将此控件呈现给指定的输出参数。
     49        /// </summary>
     50        /// <param name="output"> 要写出到的 HTML 编写器 </param>
     51        /// 

     52        //既然不需要撰写控件,将其委托回去
     53        protected override void Render(HtmlTextWriter output)
     54        {
     55            //output.Write(Text);
     56            
     57            EnsureChildControls();
     58            this.txtMath.Width=this.Width;
     59            double dHeight=this.Height.Value-butSum.Height.Value;
     60            this.txtMath.Height=Unit.Parse(dHeight.ToString());
     61            base.Render(output);
     62            
     63        }

     64        //控件的属性和方法
     65        [DefaultValue("0")]
     66        public string Text
     67        {
     68            get
     69            {
     70                //确保子控件存在
     71                EnsureChildControls();
     72                return this.txtMath.Text;
     73            }

     74            set
     75            {
     76                EnsureChildControls();
     77                //设置textbox控件的文本
     78            }

     79        }

     80        char[] strSep={'\r'};
     81        public string [] Values
     82
     83        {
     84            get
     85            {
     86                EnsureChildControls();
     87                return txtMath.Text.Split(strSep);
     88
     89            }

     90            set
     91            {
     92                EnsureChildControls();
     93                this.txtMath.Text=String.Join(" ",value);
     94
     95            }

     96        }

     97        public void Sum()
     98        {
     99            EnsureChildControls();
    100            if(txtMath.Text.Length!=0)
    101            {
    102                string [] arrNums;
    103                arrNums=txtMath.Text.Split(strSep);
    104                double dblSum=0;
    105                //把数组中的每个元素想加在一起
    106                foreach(string strCount in arrNums)
    107                {
    108                    try
    109                    {
    110                        dblSum+=Convert.ToDouble(strCount);
    111
    112                    }

    113                    catch
    114                    {
    115
    116                    }

    117                    
    118                    
    119                }

    120                //在标签中显示结果
    121                this.lblResult.Text=dblSum.ToString();
    122                
    123            }

    124            else
    125                this.lblResult.Text="0";
    126        }

    127        //在自定义控件的类中添加公共事件声明
    128        //用该控件的事件和方法从自定义控件的代码中引发事件.
    129
    130        public event EventHandler Click;
    131        
    132        void butSum_Click(object sender, EventArgs e)
    133        {
    134            this.Sum();
    135            OnClick(EventArgs.Empty);
    136            
    137        }

    138        protected virtual void OnClick(EventArgs e)
    139        {
    140            Click(this,e);
    141        }

    142    }

    143}

    144




<%@ Register TagPrefix="Custom" NameSpace="csMathControl" Assembly="csMathControl"%><%@ Register TagPrefix="Custom" NameSpace="csMathControl" Assembly="csMathControl"%><%@ Register TagPrefix="Custom" NameSpace="csMathControl" Assembly="csMathControl"%><%@ Register TagPrefix="Custom" NameSpace="csMathControl" Assembly="csMathControl"%>
posted on 2006-03-23 17:11  kuning的程序博客  阅读(380)  评论(0)    收藏  举报