项目中用了许多UC(User Control),其中有些主页需要知道UC做了某个Event后,进行联动动作.
例如:如果UC的Button控件被Click,那么主页(Page)要做一个显示不同信息的动作.
因为主页(Page)不能直接知道UC里Button被Click的事件,这个时候就用到了事件代理.
【步骤】
①为UC添加事件代理
public event EventHandler AX;
②在appropriate触发条件处添加事件
if (AX != null)
{
AX(this, e);
//Or use the following sentence code.
//AX(this, new EventArgs());
}
{
AX(this, e);
//Or use the following sentence code.
//AX(this, new EventArgs());
}
③添加主页(Page)要执行的事件
protected void Event_AX(object sender, EventArgs e)
{
Response.Write("Event has occur!<br/>");
}
{
Response.Write("Event has occur!<br/>");
}
④在主页(Page)上添加事件代理,从而执行主页上的Function
方法1:*.CS端
UC_AXzhz.AX += new EventHandler(Event_AX);
方法2:*.aspx端【注:使用此方法,Event_AX必须为protected/interanl/public类型,不能为private类型】
<AX:UC_AX ID="UC_AXzhz" OnAX="Event_AX" runat="server" />
【完整代码】
UC前端:
UC后台:
Page前端:
Page后台:
博客园→斧头帮少帮主