惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI
月光博客
月光博客
F
Fortinet All Blogs
Stack Overflow Blog
Stack Overflow Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
博客园 - 司徒正美
P
Proofpoint News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
L
LangChain Blog
博客园 - 聂微东
G
Google Developers Blog
博客园 - Franky

博客园 - 斧头帮少帮主

猜一猜,会输出什么? ASP.NET错误处理的两种方案_AX Log4Net最简单的使用配置_AX 一个修改后的JS日历(Calendar)控件_AX - 斧头帮少帮主 - 博客园 页面回传(PostBack)后恢复滚动条的位置_AX - 斧头帮少帮主 - 博客园 Hidden Field 的值不能回传(PostBack)到后台_AX Delegate(代理)异常:该委托必须有一个目标 WCF学习随笔6--Version变更--我们在前进_AX WCF学习随笔5--我知道:Known--可憐的孩子_AX WCF学习随笔4--契约|序列化详解--祭斧_AX WCF学习随笔3--第一个Sample--祭斧_AX - 斧头帮少帮主 - 博客园 WCF学习随笔2--Contract--磨斧_AX WCF学习随笔1--扫盲--磨斧_AX 把项目运行情况写入系统日志(Log)的三种方法【续】_AX 把项目运行情况写入系统日志(Log)的三种方法_AX XML常用操作总结_AX DataTable常用操作总结_AX - 斧头帮少帮主 - 博客园 .NET(C#)代码性能优化_AX - 斧头帮少帮主 - 博客园 Panel的不足及弥补_AX - 斧头帮少帮主 - 博客园
为用户控件User Control添加事件_AX
斧头帮少帮主 · 2007-11-02 · via 博客园 - 斧头帮少帮主

【引】
项目中用了许多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());
        }


③添加主页(Page)要执行的事件

 protected void Event_AX(object sender, EventArgs e)
    {
        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前端:

<%@ 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/>");
    }


}

博客园斧头帮少帮主