2.0下复合控件的简单开发

看了一下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等。实现的功能有,用户提交时,客户端判断是否已录入用户名,邮箱。。提交后触发一个自定义的事件。小弟才疏学浅,如有问题,欢迎赐教。。。点击这里下载源码


代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Permissions;
using System.ComponentModel;
namespace WebControls
{

   
/// <summary>
   
/// 获取控件事件的参数类。。
   
/// </summary>

   public class SubmitEventArgs
   
{
       
public SubmitEventArgs(string email, string name)
       
{
           
this.email = email;
           
this.name = name;
       }

       
public SubmitEventArgs() { }
       
private string email,name;
       
public string Email
       
{
           
get{
               
return email;
           }

           
set { email = value; }
       }


       
public string Name
       
{
           
get{
               
return name;
           }

           
set{
               name
=value;
           }

       
       }

   }
     

    
/// <summary>
    
/// 复合控件Register
    
/// </summary>

    [ToolboxData("<{0}:Register runat=server></{0}:Register>")]
    [DefaultEvent(
"Submit")]
    [DefaultProperty(
"ButtonText")]
    
public class Register : CompositeControl
    
{
        
public Register() { }

        
public Button submitButton, resetButton;
        
private TextBox nameTextBox, emailTextBox;
        
private Label nameLabel, emailLabel;
        
private RequiredFieldValidator emailValidator, nameValidator;
        
private static readonly object EventSubmitKey = new object();

     
         
public delegate void SubmiteEventHandler(object sender, SubmitEventArgs e);

        
"Property"

        
"Event"


        
/// <summary>
        
/// 2.0新增的方法。。
        
/// </summary>

        protected override void RecreateChildControls()
        
{
            
base.RecreateChildControls();
            EnsureChildControls();
        }


        
protected override void CreateChildControls()
        
{
            Controls.Clear();
            nameLabel 
= new Label();
            nameTextBox 
= new TextBox();
            nameTextBox.ID 
= "nameTextbox";

            nameValidator 
= new RequiredFieldValidator();
            nameValidator.ID 
= "validator1";
            nameValidator.ControlToValidate 
= nameTextBox.ID;
            nameValidator.Text 
= "帐户不能为空";
            nameValidator.Display 
= ValidatorDisplay.Static;


            emailLabel 
= new Label();
            emailTextBox 
= new TextBox();
            emailTextBox.ID 
= "emailTextBox";

            emailValidator 
= new RequiredFieldValidator();
            emailValidator.ID 
= "validator2";
           
            emailValidator.ErrorMessage 
= "邮件不能为空";
           
            emailValidator.ControlToValidate 
= emailTextBox.ID;
            emailValidator.Display 
= ValidatorDisplay.Static;


            submitButton 
= new Button();
            submitButton.ID 
= "button1";
            submitButton.Text 
= "登入";
            submitButton.ValidationGroup 
= this.ID;
            nameValidator.ValidationGroup 
= this.ID;
            emailValidator.ValidationGroup 
= this.ID;
            submitButton.Click 
+= new EventHandler(submitButton_Click);



            Controls.Add(nameLabel);
            Controls.Add(nameTextBox);
            Controls.Add(nameValidator);
            Controls.Add(emailLabel);
            Controls.Add(emailTextBox);
            Controls.Add(emailValidator);
            Controls.Add(submitButton);


        }


        
void submitButton_Click(object sender, EventArgs e)
        
{
            OnSubmit(e);
        }



        
protected override void RenderContents(HtmlTextWriter writer)
        
{
          
//  base.RenderContents(writer);
            AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Width, 
"100%");
            writer.AddAttribute(HtmlTextWriterAttribute.Border, 
"1");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, 
"1"false);
            writer.RenderBeginTag(HtmlTextWriterTag.Table);


            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            nameLabel.RenderControl(writer);
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            nameTextBox.RenderControl(writer);
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            nameValidator.RenderControl(writer);
            writer.Write(
"&nbsp");
            writer.RenderEndTag();

            writer.RenderEndTag();



            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            emailLabel.RenderControl(writer);
            writer.RenderEndTag();


            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            emailTextBox.RenderControl(writer);
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            emailValidator.RenderControl(writer);
            writer.Write(
"&nbsp");
            writer.RenderEndTag();

            writer.RenderEndTag();


            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, 
"2"false);
            writer.AddAttribute(HtmlTextWriterAttribute.Align, 
"right"false);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            submitButton.RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(
"&nbsp");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderEndTag();

        }




    }

}


我是直接放入App_Code文件夹里面的。这样做的好处是不用编译都可以调用。
测试页default.aspx,代码如下:
<%@ Page Language="C#"  EnableViewState="false" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register  TagPrefix="index"  Namespace="WebControls"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">        
        
<index:Register runat=server EmailLabelText="邮件" ID=reg NameLabelText="名称"  SubmitButtonText="登入"  Email="Genson_diy@sina.com" OnSubmit="reg_Submit" />
    
</form>
</body>
</html>
cs 代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
       
    }


    
protected void reg_Submit(object sender, SubmitEventArgs e)
    
{
        Response.Write(e.Name 
+ e.Email);
        
    }

}

posted @ 2006-04-10 09:53  永不言败  阅读(901)  评论(0编辑  收藏  举报