2.0下复合控件的简单开发

本文介绍如何在ASP.NET 2.0中使用CompositeControl开发自定义控件,通过实例演示创建一个包含文本框、按钮及验证控件的注册表单复合控件。

看了一下Net2.0的Sdk,于是就写下了这篇文章,2.0下复合控件的简单开发。文章是给初学者看的。。高手就跳过吧。


2.0下面多了个抽象类 CompositeControl,这个类是继承了WebControl的,并实现了INamingContainer,ICompositeControlDesignerAccessor接口。这个跟1.X有点不一样,当然,我们也可以继承WebControl来写复合控件。
其中:INamingContainer的作用是保证子控件的ID是唯一的。
ICompositeControlDesignerAccessor一看就知道是设计是用的。
2.0里面的一些复合控件,如Login,Wizard都是基础这个CompositeControl开发的。
CompositeControl重写了WebControl的Controls属性,DataBind方法。其中DataBind还有重载方法。
下面简单的写出一个注册复合控件,复合控件是包括TextBox,Button,RequiredFieldValidator,Label等。实现的功能有,用户提交时,客户端判断是否已录入用户名,邮箱。。提交后触发一个自定义的事件。小弟才疏学浅,如有问题,欢迎赐教。。。点击这里下载源码


代码如下:

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Security.Permissions;
None.gif
using  System.ComponentModel;
None.gif
namespace  WebControls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// <summary>
InBlock.gif   
/// 获取控件事件的参数类。。
ExpandedSubBlockEnd.gif   
/// </summary>

InBlock.gif   public class SubmitEventArgs
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif       
public SubmitEventArgs(string email, string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif           
this.email = email;
InBlock.gif           
this.name = name;
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockStart.gifContractedSubBlock.gif       
public SubmitEventArgs() dot.gif{ }
InBlock.gif       
private string email,name;
InBlock.gif       
public string Email
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif           
getdot.gif{
InBlock.gif               
return email;
ExpandedSubBlockEnd.gif           }

ExpandedSubBlockStart.gifContractedSubBlock.gif           
set dot.gif{ email = value; }
ExpandedSubBlockEnd.gif       }

InBlock.gif
InBlock.gif       
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif           
getdot.gif{
InBlock.gif               
return name;
ExpandedSubBlockEnd.gif           }

ExpandedSubBlockStart.gifContractedSubBlock.gif           
setdot.gif{
InBlock.gif               name
=value;
ExpandedSubBlockEnd.gif           }

InBlock.gif       
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif   }
     
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 复合控件Register
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [ToolboxData("<{0}:Register runat=server></{0}:Register>")]
InBlock.gif    [DefaultEvent(
"Submit")]
InBlock.gif    [DefaultProperty(
"ButtonText")]
InBlock.gif    
public class Register : CompositeControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Register() dot.gif{ }
InBlock.gif
InBlock.gif        
public Button submitButton, resetButton;
InBlock.gif        
private TextBox nameTextBox, emailTextBox;
InBlock.gif        
private Label nameLabel, emailLabel;
InBlock.gif        
private RequiredFieldValidator emailValidator, nameValidator;
InBlock.gif        
private static readonly object EventSubmitKey = new object();
InBlock.gif
InBlock.gif     
InBlock.gif         
public delegate void SubmiteEventHandler(object sender, SubmitEventArgs e);
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
"Property"#region"Property"
InBlock.gif        [Bindable(
true)]
InBlock.gif        [Category(
"Appearance")]
InBlock.gif        [DefaultValue(
"登入")]
InBlock.gif        [Description(
"登入按钮的标签文字")]
InBlock.gif        
public string SubmitButtonText
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                
return submitButton.Text;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                submitButton.Text 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        [Bindable(
true)]
InBlock.gif        [Category(
"Appearance")]
InBlock.gif        [DefaultValue(
"")]
InBlock.gif        [Description(
"注册帐户")]
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                
return nameTextBox.Text;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                nameTextBox.Text 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        [Bindable(
true)]
InBlock.gif        [Category(
"Appearance")]
InBlock.gif        [DefaultValue(
"")]
InBlock.gif        [Description(
"帐户错误信息")]
InBlock.gif        
public string NameErrorMessage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                
return nameValidator.ErrorMessage;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                nameValidator.ErrorMessage 
= value;
InBlock.gif                nameValidator.ToolTip 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Bindable(
true)]
InBlock.gif        [Category(
"Appearance")]
InBlock.gif        [DefaultValue(
"帐户")]
InBlock.gif        [Description(
"帐户标签")]
InBlock.gif        
public string NameLabelText
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                
return nameLabel.Text;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                nameLabel.Text 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Bindable(
true)]
InBlock.gif        [Category(
"Appearance")]
InBlock.gif        [DefaultValue(
"")]
InBlock.gif        [Description(
"邮件")]
InBlock.gif        
public string Email
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                
return emailTextBox.Text;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                emailTextBox.Text 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        [Bindable(
true)]
InBlock.gif        [Category(
"Appearance")]
InBlock.gif        [DefaultValue(
"")]
InBlock.gif        [Description(
"邮件错误信息")]
InBlock.gif        
public string EmailErrorMessage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                
return emailValidator.ErrorMessage;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                emailValidator.ErrorMessage 
= value;
InBlock.gif                emailValidator.ToolTip 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Bindable(
true)]
InBlock.gif        [Category(
"Appearance")]
InBlock.gif        [DefaultValue(
"邮件")]
InBlock.gif        [Description(
"邮件标签名称")]
InBlock.gif        
public string EmailLabelText
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                
return emailLabel.Text;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EnsureChildControls();
InBlock.gif                emailLabel.Text 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
"Event"#region"Event"
InBlock.gif        [Category(
"Action")]
InBlock.gif        [Description(
"Raised When the user clicks the button")]
InBlock.gif        
public event SubmiteEventHandler Submit
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            add
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Events.AddHandler(EventSubmitKey, value);
ExpandedSubBlockEnd.gif            }

InBlock.gif            remove
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Events.RemoveHandler(EventSubmitKey, value);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void OnSubmit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SubmiteEventHandler submitHandler 
= (SubmiteEventHandler)Events[EventSubmitKey];
InBlock.gif            
if (submitHandler != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                submitHandler(
thisnew SubmitEventArgs(this.Email, this.Name));
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 2.0新增的方法。。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void RecreateChildControls()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.RecreateChildControls();
InBlock.gif            EnsureChildControls();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void CreateChildControls()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Controls.Clear();
InBlock.gif            nameLabel 
= new Label();
InBlock.gif            nameTextBox 
= new TextBox();
InBlock.gif            nameTextBox.ID 
= "nameTextbox";
InBlock.gif
InBlock.gif            nameValidator 
= new RequiredFieldValidator();
InBlock.gif            nameValidator.ID 
= "validator1";
InBlock.gif            nameValidator.ControlToValidate 
= nameTextBox.ID;
InBlock.gif            nameValidator.Text 
= "帐户不能为空";
InBlock.gif            nameValidator.Display 
= ValidatorDisplay.Static;
InBlock.gif
InBlock.gif
InBlock.gif            emailLabel 
= new Label();
InBlock.gif            emailTextBox 
= new TextBox();
InBlock.gif            emailTextBox.ID 
= "emailTextBox";
InBlock.gif
InBlock.gif            emailValidator 
= new RequiredFieldValidator();
InBlock.gif            emailValidator.ID 
= "validator2";
InBlock.gif           
InBlock.gif            emailValidator.ErrorMessage 
= "邮件不能为空";
InBlock.gif           
InBlock.gif            emailValidator.ControlToValidate 
= emailTextBox.ID;
InBlock.gif            emailValidator.Display 
= ValidatorDisplay.Static;
InBlock.gif
InBlock.gif
InBlock.gif            submitButton 
= new Button();
InBlock.gif            submitButton.ID 
= "button1";
InBlock.gif            submitButton.Text 
= "登入";
InBlock.gif            submitButton.ValidationGroup 
= this.ID;
InBlock.gif            nameValidator.ValidationGroup 
= this.ID;
InBlock.gif            emailValidator.ValidationGroup 
= this.ID;
InBlock.gif            submitButton.Click 
+= new EventHandler(submitButton_Click);
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif            Controls.Add(nameLabel);
InBlock.gif            Controls.Add(nameTextBox);
InBlock.gif            Controls.Add(nameValidator);
InBlock.gif            Controls.Add(emailLabel);
InBlock.gif            Controls.Add(emailTextBox);
InBlock.gif            Controls.Add(emailValidator);
InBlock.gif            Controls.Add(submitButton);
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void submitButton_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OnSubmit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
protected override void RenderContents(HtmlTextWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          
//  base.RenderContents(writer);
InBlock.gif
            AddAttributesToRender(writer);
InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Width, 
"100%");
InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Border, 
"1");
InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, 
"1"false);
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Table);
InBlock.gif
InBlock.gif
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif            nameLabel.RenderControl(writer);
InBlock.gif            writer.RenderEndTag();
InBlock.gif
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif            nameTextBox.RenderControl(writer);
InBlock.gif            writer.RenderEndTag();
InBlock.gif
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif            nameValidator.RenderControl(writer);
InBlock.gif            writer.Write(
"&nbsp");
InBlock.gif            writer.RenderEndTag();
InBlock.gif
InBlock.gif            writer.RenderEndTag();
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif            emailLabel.RenderControl(writer);
InBlock.gif            writer.RenderEndTag();
InBlock.gif
InBlock.gif
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif            emailTextBox.RenderControl(writer);
InBlock.gif            writer.RenderEndTag();
InBlock.gif
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif            emailValidator.RenderControl(writer);
InBlock.gif            writer.Write(
"&nbsp");
InBlock.gif            writer.RenderEndTag();
InBlock.gif
InBlock.gif            writer.RenderEndTag();
InBlock.gif
InBlock.gif
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, 
"2"false);
InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Align, 
"right"false);
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif            submitButton.RenderControl(writer);
InBlock.gif            writer.RenderEndTag();
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif            writer.Write(
"&nbsp");
InBlock.gif            writer.RenderEndTag();
InBlock.gif            writer.RenderEndTag();
InBlock.gif
InBlock.gif            writer.RenderEndTag();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

我是直接放入App_Code文件夹里面的。这样做的好处是不用编译都可以调用。
测试页default.aspx,代码如下:
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Language="C#"  EnableViewState="false" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"  %>
ExpandedBlockStart.gifContractedBlock.gif
<% dot.gif @ Register  TagPrefix="index"  Namespace="WebControls"   %>
None.gif
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
None.gif
None.gif
< html  xmlns ="http://www.w3.org/1999/xhtml"   >
None.gif
< head  runat ="server" >
None.gif    
< title > Untitled Page </ title >
None.gif
</ head >
None.gif
< body >
None.gif    
< form  id ="form1"  runat ="server" >         
None.gif        
< index:Register  runat =server  EmailLabelText ="邮件"  ID =reg  NameLabelText ="名称"   SubmitButtonText ="登入"   Email ="Genson_diy@sina.com"  OnSubmit ="reg_Submit"   />
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >
None.gif
cs 代码如下:
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  WebControls;
None.gif
public  partial  class  _Default : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
protected void reg_Submit(object sender, SubmitEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Response.Write(e.Name 
+ e.Email);
InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/genson/archive/2006/04/10/371039.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值