博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ASP.NET复合控件开发

Posted on 2011-06-28 20:31  itcfj  阅读(183)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ServerControl1
{
[DefaultProperty(
"Text")]
[ToolboxData(
"<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
public class ServerControl1 : CompositeControl,INamingContainer
{
[Bindable(
true)]
[Category(
"Appearance")]
[DefaultValue(
"")]
[Localizable(
true)]
public string Text
{
get
{
String s
= (String)ViewState["Text"];
return ((s == null) ? "[" + this.ID + "]" : s);
}

set
{
ViewState[
"Text"] = value;
}
}

public string Tip
{
get { return ViewState["aa"].ToString(); }
set{ViewState["aa"]=value;}
}
TextBox tb1;
Button btn;
Label lb;
protected override void CreateChildControls()
{
Controls.Add(
new LiteralControl("<h3>第一个数"));
tb1
= new TextBox();
tb1.Text
= "0";
Controls.Add(tb1);
btn
= new Button();
btn.Text
= "登录";
btn.CommandName
= "login";
// btn.Click += new EventHandler(btn_Click);
Controls.Add(btn);
lb
= new Label();
lb.Text
= "aaaaaaaaa";
Controls.Add(lb);
}
//点击复合控件中的子控件的事件时触发,事件冒泡出来

protected override bool OnBubbleEvent(object source, EventArgs e)

{
bool handed = false;
if (e is CommandEventArgs )
{
CommandEventArgs ce
= (CommandEventArgs)e;
if (ce.CommandName=="login")
{
lb.Text
= "aaaaaaaa";
handed
= true;
}
}
return handed;
}

public class CheckEventArgs : EventArgs
{
private bool match = false;

public CheckEventArgs(int difference)
{
if (difference == 0)
{
match
= true;
}
}
public bool Match
{
get
{
return match;
}
}
}
public event CheckEventHandler Check;
public delegate void CheckEventHandler(object sender, CheckEventArgs ce);
}
}