using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace CustomControls
{
/// <summary>
/// WebCustomControl1 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:Composite runat=server></{0}:Composite>")]
public class Composite : Control, INamingContainer
//标识在 Page 对象的控件层次结构内创建新 ID 命名空间的容器控件。这仅是一个标记接口。
{
private int number = 100;
private Label label;
public int Number
{
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
{
get
{
EnsureChildControls();
return label.Text;
}
set
{
EnsureChildControls();
label.Text = value;
}
}
public event CheckEventHandler Check;
protected virtual void OnCheck(CheckEventArgs ce)
{
if (Check != null)
{
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 CheckEventArgs(Sum - Number));
}
}
public class CheckEventArgs : EventArgs
{
private bool match = false;
public CheckEventArgs (int difference)
{
if (difference == 0)
{
match = true;
}
}
public bool Match
{
get
{
return match;
}
}
}
public delegate void CheckEventHandler(object sender, CheckEventArgs ce);
}
--------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace CompositeEvent
{
/// <summary>
/// WebCustomControl1 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:EventBubbler runat=server></{0}:EventBubbler>")]
public class EventBubbler : Control, INamingContainer
{
private int number = 100;
private Label label;
private TextBox box1;
private TextBox box2;
public event EventHandler Click;
public event EventHandler Reset;
public event EventHandler Submit;
public string Label
{
get
{
EnsureChildControls();
return label.Text;
}
set
{
EnsureChildControls();
label.Text = value;
}
}
public int Number
{
get
{
return number;
}
set
{
number = value;
}
}
public string Text1
{
get
{
EnsureChildControls();
return box1.Text;
}
set
{
EnsureChildControls();
box1.Text = value;
}
}
public string Text2
{
get
{
EnsureChildControls();
return box2.Text;
}
set
{
EnsureChildControls();
box2.Text = value;
}
}
protected override void CreateChildControls()
{
Controls.Add(new LiteralControl("<h3>第一个数字 : "));
box1 = new TextBox();
box1.Text = "0";
Controls.Add(box1);
Controls.Add(new LiteralControl("</h3>"));
Controls.Add(new LiteralControl("<h3>第二个数字: "));
box2 = new TextBox();
box2.Text = "0";
Controls.Add(box2);
Controls.Add(new LiteralControl("</h3>"));
Button button1 = new Button();
button1.Text = "点击";
button1.CommandName = "Click";
Controls.Add(button1);
Button button2 = new Button();
button2.Text = "重置";
button2.CommandName = "Reset";
Controls.Add(button2);
Button button3 = new Button();
button3.Text = "提交";
button3.CommandName = "Submit";
Controls.Add(button3);
Controls.Add(new LiteralControl("<br><br>"));
label = new Label();
label.Height = 50;
label.Width = 500;
label.Text = "单击一个按钮";
Controls.Add(label);
}
protected override bool OnBubbleEvent(object source, EventArgs e) //事件冒泡
{
bool handled = false;
if (e is CommandEventArgs)
{
CommandEventArgs ce = (CommandEventArgs)e;
if (ce.CommandName == "Click")
{
OnClick(ce);
handled = true;
}
else if (ce.CommandName == "Reset")
{
OnReset(ce);
handled = true;
}
else if (ce.CommandName == "Submit")
{
OnSubmit(ce);
handled = true;
}
}
return handled;
}
protected virtual void OnClick (EventArgs e)
{
if (Click != null)
{
Click(this,e);
}
}
protected virtual void OnReset (EventArgs e)
{
if (Reset != null)
{
Reset(this,e);
}
}
protected virtual void OnSubmit (EventArgs e)
{
if (Submit != null)
{
Submit(this,e);
}
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Xml;
using System.Collections;
namespace CompositeInput
{
/// <summary>
/// WebCustomControl1 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:ComInput runat=server></{0}:ComInput>")]
public class ComInput : Control, INamingContainer
//标识在 Page 对象的控件层次结构内创建新 ID 命名空间的容器控件。这仅是一个标记接口。
{
private string strFileName = "bad_words.xml";
private Label label;
public string FileName
{
get
{
return strFileName;
}
set
{
strFileName = value;
}
}
//以用户提交的文本内容为输入参数。如果用户提交的内容包含攻击性言辞,
//则返回修改后的版本,
//否则,直接返回原始的文本。
public string CheckString(string InputString)
{
ArrayList alWordList = new ArrayList();
string xmlDocPath = this.Page.Server.MapPath("bad_words.xml");
XmlTextReader xmlReader = new XmlTextReader(xmlDocPath);
string asterisks = "*************************";
// 将定义攻击性言辞的xml文件内容读入到一个ArrayList
while (xmlReader.Read())
{
if(xmlReader.NodeType==XmlNodeType.Text)
alWordList.Add(xmlReader.Value);
}
xmlReader.Close();
//检查用户提交的文本内容,将攻击性言辞替换为适当数量的星号
foreach(string element in alWordList)
InputString=InputString.Replace(element,asterisks.Substring(1, (element.Length)));
return InputString ;
}
public string Text
{
get
{
EnsureChildControls();
return label.Text;
}
set
{
EnsureChildControls();
label.Text = value;
}
}
public event CheckEventHandler Check;
protected virtual void OnCheck(CheckEventArgs ce)
{
if (Check != null)
{
Check(this,ce);
}
}
protected override void CreateChildControls()
{
Controls.Add(new LiteralControl("<h3>请在下面输入文字内容: "));
TextBox box1 = new TextBox();
box1.Text = "";
Controls.Add(box1);
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 = "";
}
private void ButtonClicked(Object sender, EventArgs e)
{
OnCheck(new CheckEventArgs(((TextBox)Controls[1]).Text,CheckString(((TextBox)Controls[1]).Text)));
}
}
public class CheckEventArgs : EventArgs
{
private bool match = false;
public CheckEventArgs (string str1,string str2)
{
if (str1 == str2)
{
match = true;
}
}
public bool Match
{
get
{
return match;
}
}
}
public delegate void CheckEventHandler(object sender, CheckEventArgs ce);
}