// Composite.cs.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace CustomControls
{
public class Composite : Control, INamingContainer
{
private int number = 100;
private Label label;
public int Number //PPP1
{
get
{
return number;
}
set
{
number = value;
}
}
private int Sum
{
get
{
EnsureChildControls();
return Int32.Parse(((TextBox)Controls[1]).Text) +
Int32.Parse(((TextBox)Controls[4]).Text);
}
} public string Text //PPP2
{
get
{
EnsureChildControls();
return label.Text;
}
set
{
EnsureChildControls();
label.Text = value;
}
}
public event CheckEventHandler2 Check; //PPP3 这一行可理解为定义一个事件类CheckEventHandler2的实例Check吗?
protected virtual void OnCheck(CheckEventArgs2 ce)
{
if (Check != null) //这句是不是查看有无创建Check实例?
{
Check(this,ce); // 这个调用了实例那个方法?
}
}
protected override void CreateChildControls()
{
Controls.Add(new LiteralControl("<h3>第一个数 : "));
TextBox box1 = new TextBox();
box1.Text = "0";
Controls.Add(box1);
Controls.Add(new LiteralControl("</h3>"));
Controls.Add(new LiteralControl("<h3>第二个数 : "));
TextBox box2 = new TextBox();
box2.Text = "0";
Controls.Add(box2);
Controls.Add(new LiteralControl("</h3>"));
Button button1 = new Button();
button1.Text = "提交";
Controls.Add(new LiteralControl("<br>"));
Controls.Add(button1);
button1.Click += new EventHandler(this.ButtonClicked);
Controls.Add(new LiteralControl("<br><br>"));
label = new Label();
label.Height = 50;
label.Width = 500;
label.Text = "输入数后占击按钮提交.";
Controls.Add(label);
}
protected override void OnPreRender(EventArgs e)
{
((TextBox)Controls[1]).Text = "0";
((TextBox)Controls[4]).Text = "0";
}
private void ButtonClicked(Object sender, EventArgs e)
{
OnCheck(new CheckEventArgs2(Sum - Number));
}
}
}// CheckEvent.cs.
// Contains the code for the custom event data class CheckEventArgs.
// Also defines the event handler for the Check event.
using System;namespace CustomControls
{
public class CheckEventArgs2 : EventArgs
{
private bool match = false;
public CheckEventArgs2 (int difference)
{
if (difference == 0)
{
match = true;
}
}
public bool Match
{
get
{
return match;
}
}
}
public delegate void CheckEventHandler2(object sender, CheckEventArgs2 ce);//这一句有什么用,ce是什么?
}还有就是为什么要把判断Sum - Number是否等于零这个简单操作要写成一个类呢?作用是什么?
应用层页:
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<html>
<script language="C#" runat=server> private void Sum_Checked2 (Object sender,CheckEventArgs2 e) //这里为什么要有:Object sender,CheckEventArgs2 e
{
if (e.Match == true)
Composite3.Text = "<h2>通过啦!!! </h2>";
else
Composite3.Text = "对不起,你输入的两个数加起来不等于" +
"那隐藏数."; }
</script>
<body><h1> The Mystery Sum Game </h1><br>
<form runat=server>
<Custom:Composite id = "Composite3" OnCheck = "Sum_Checked2" runat = server/>
</form>
</body>
</html>
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace CustomControls
{
public class Composite : Control, INamingContainer
{
private int number = 100;
private Label label;
public int Number //PPP1
{
get
{
return number;
}
set
{
number = value;
}
}
private int Sum
{
get
{
EnsureChildControls();
return Int32.Parse(((TextBox)Controls[1]).Text) +
Int32.Parse(((TextBox)Controls[4]).Text);
}
} public string Text //PPP2
{
get
{
EnsureChildControls();
return label.Text;
}
set
{
EnsureChildControls();
label.Text = value;
}
}
public event CheckEventHandler2 Check; //PPP3 这一行可理解为定义一个事件类CheckEventHandler2的实例Check吗?
protected virtual void OnCheck(CheckEventArgs2 ce)
{
if (Check != null) //这句是不是查看有无创建Check实例?
{
Check(this,ce); // 这个调用了实例那个方法?
}
}
protected override void CreateChildControls()
{
Controls.Add(new LiteralControl("<h3>第一个数 : "));
TextBox box1 = new TextBox();
box1.Text = "0";
Controls.Add(box1);
Controls.Add(new LiteralControl("</h3>"));
Controls.Add(new LiteralControl("<h3>第二个数 : "));
TextBox box2 = new TextBox();
box2.Text = "0";
Controls.Add(box2);
Controls.Add(new LiteralControl("</h3>"));
Button button1 = new Button();
button1.Text = "提交";
Controls.Add(new LiteralControl("<br>"));
Controls.Add(button1);
button1.Click += new EventHandler(this.ButtonClicked);
Controls.Add(new LiteralControl("<br><br>"));
label = new Label();
label.Height = 50;
label.Width = 500;
label.Text = "输入数后占击按钮提交.";
Controls.Add(label);
}
protected override void OnPreRender(EventArgs e)
{
((TextBox)Controls[1]).Text = "0";
((TextBox)Controls[4]).Text = "0";
}
private void ButtonClicked(Object sender, EventArgs e)
{
OnCheck(new CheckEventArgs2(Sum - Number));
}
}
}// CheckEvent.cs.
// Contains the code for the custom event data class CheckEventArgs.
// Also defines the event handler for the Check event.
using System;namespace CustomControls
{
public class CheckEventArgs2 : EventArgs
{
private bool match = false;
public CheckEventArgs2 (int difference)
{
if (difference == 0)
{
match = true;
}
}
public bool Match
{
get
{
return match;
}
}
}
public delegate void CheckEventHandler2(object sender, CheckEventArgs2 ce);//这一句有什么用,ce是什么?
}还有就是为什么要把判断Sum - Number是否等于零这个简单操作要写成一个类呢?作用是什么?
应用层页:
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<html>
<script language="C#" runat=server> private void Sum_Checked2 (Object sender,CheckEventArgs2 e) //这里为什么要有:Object sender,CheckEventArgs2 e
{
if (e.Match == true)
Composite3.Text = "<h2>通过啦!!! </h2>";
else
Composite3.Text = "对不起,你输入的两个数加起来不等于" +
"那隐藏数."; }
</script>
<body><h1> The Mystery Sum Game </h1><br>
<form runat=server>
<Custom:Composite id = "Composite3" OnCheck = "Sum_Checked2" runat = server/>
</form>
</body>
</html>
---------------------------------------
声名一委托,ce是 CheckEventArgs的实例,从EventArgs派生---------------------------------------------------
public event CheckEventHandler2 Check; //PPP3 这一行可理解为定义一个事件类CheckEventHandler2的实例Check吗?
----------------------------------
声明一CheckEventHandler2委托类的事件还有就是为什么要把判断Sum - Number是否等于零这个简单操作要写成一个类呢?作用是什么?
-------------------------------------------是手段而不是目的
Y就是一的关于委托,事件小例子