为用户控件User Control添加事件_AX
【引】
项目中用了许多UC(User Control),其中有些主页需要知道UC做了某个Event后,进行联动动作.
例如:如果UC的Button控件被Click,那么主页(Page)要做一个显示不同信息的动作.
因为主页(Page)不能直接知道UC里Button被Click的事件,这个时候就用到了事件代理.
【步骤】
①为UC添加事件代理
②在appropriate触发条件处添加事件
③添加主页(Page)要执行的事件
④在主页(Page)上添加事件代理,从而执行主页上的Function
方法1:*.CS端
方法2:*.aspx端【注:使用此方法,Event_AX必须为protected/interanl/public类型,不能为private类型】
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UC_AX.ascx.cs" Inherits="UC_AX" %>
<asp:Button ID="btnClick" runat="server" OnClick="btnClick_Click" Style="z-index: 100;
left: 156px; position: absolute; top: 113px" Text="ClickMe" Height="25px" Width="74px" />
UC后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UC_AX : System.Web.UI.UserControl
{
//①添加事件代理
public event EventHandler AX;

protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnClick_Click(object sender, EventArgs e)
{
//②如果点击了Button, Fire the代理事件
//可以通过多种条件fire事件代理.
//Must add this condition, Otherwise,if AX equal null
//Will throw a exception
if (AX != null)
{
AX(this, e);
//Or use the following sentence code.
//AX(this, new EventArgs());
}
}
}
Page前端:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default_AX.aspx.cs" Inherits="Default_AX" %>

<%@ Register Src="UC_AX.ascx" TagName="UC_AX" TagPrefix="AX" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<!--//④Another way:为代理添加需要执行的事件-->
<AX:UC_AX ID="UC_AXzhz" OnAX="Event_AX" runat="server" />
</div>
</form>
</body>
</html>
Page后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default_AX : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
//③为代理添加需要执行的事件
UC_AXzhz.AX += new EventHandler(Event_AX);
}

//⑤当UC的Button被Click后,主页上要执行的动作
protected void Event_AX(object sender, EventArgs e)
{
Response.Write("Event has occur!<br/>");
}

}
博客园→斧头帮少帮主
项目中用了许多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后台:



























博客园→斧头帮少帮主
少帮主的斧头好久不饮血了!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2006-11-02 使用IHttpHandler接口实现【不同路径+任意URL后缀重写到指定页面且URL地址不变】(附源码)_AX