在开发asp.net过程中,Insus.NET较喜欢写UserControl(用户控件),因为它就是一个灵活的对象。可以在网页随意变换与控制。此次Insus.NET想说的问题,可看如下说明,就比如前一篇《观察者模式与用户控件之间的互动 》,其中UserD与UserC两个用户控件可以交互。这两个用户控件都写了event(事件),delegate(委托)。这部分可以重构一下。把他们写成一个interface(接口),也就是写成一个事件接口。此篇另写例子,让我们学会如何在asp.net开发过程中写事件接口与应用,非以前篇作重构与修改。
ITransmitable using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ITransmitable
/// </summary>
public delegate void TransmitEventHandler(object sender,string value);
public interface ITransmitable
{
//事件接口
event TransmitEventHandler Transmit;
}
两个用户控件分别实作这个接口。UserA,
View Code using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UserA : System.Web.UI.UserControl, ITransmitable //实作接口
{
protected void Page_Load(object sender, EventArgs e)
{
}
public event TransmitEventHandler Transmit;
protected void Button1_Click(object sender, EventArgs e)
{
if (Transmit != null)
Transmit(this, this.TextBox1.Text);
this.TextBox1.Text = string.Empty;
}
}
UserB用户控件实作接口,
View Code using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UserB : System.Web.UI.UserControl, ITransmitable
{
protected void Page_Load(object sender, EventArgs e)
{
}
public event TransmitEventHandler Transmit;
protected void Button1_Click(object sender, EventArgs e)
{
if (Transmit != null)
Transmit(this, this.TextBox1.Text);
this.TextBox1.Text = string.Empty;
}
}
例子演示:
Demo代码:
http://download.cnblogs.com/insus/ASPDOTNET/EventInterfaceAndUserControlInteractive.rar