为用户控件 User Control添加事件(转)

原文地址:http://www.cnblogs.com/AXzhz/archive/2007/11/02/947011.html

 

【引】
项目中用了许多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前端:

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Control Language="C#" AutoEventWireup="true" CodeFile="UC_AX.ascx.cs" Inherits="UC_AX"  %>
None.gif
< asp:Button  ID ="btnClick"  runat ="server"  OnClick ="btnClick_Click"  Style ="z-index: 100;
None.gif    left: 156px; position: absolute; top: 113px"
 Text ="ClickMe"  Height ="25px"  Width ="74px"   />


UC后台:

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
public   partial   class  UC_AX : System.Web.UI.UserControl
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//①添加事件代理
InBlock.gif
    public event EventHandler AX;
InBlock.gif
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void btnClick_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//②如果点击了Button, Fire the代理事件
InBlock.gif        
//可以通过多种条件fire事件代理.
InBlock.gif        
//Must add this condition, Otherwise,if AX equal null
InBlock.gif        
//Will throw a exception
InBlock.gif
        if (AX != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            AX(
this, e);
InBlock.gif            
//Or use the following sentence code.
InBlock.gif            
//AX(this, new EventArgs());
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


Page前端:

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Language="C#" AutoEventWireup="true" CodeFile="Default_AX.aspx.cs" Inherits="Default_AX"  %>
None.gif
ExpandedBlockStart.gifContractedBlock.gif
<% dot.gif @ Register Src="UC_AX.ascx" TagName="UC_AX" TagPrefix="AX"  %>
None.gif
None.gif
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
None.gif
None.gif
< html  xmlns ="http://www.w3.org/1999/xhtml"   >
None.gif
< head  runat ="server" >
None.gif    
< title > Untitled Page </ title >
None.gif
</ head >
None.gif
< body >
None.gif    
< form  id ="form1"  runat ="server" >
None.gif    
< div >
None.gif        
<!-- //④Another way:为代理添加需要执行的事件 -->   
None.gif        
< AX:UC_AX  ID ="UC_AXzhz"  OnAX ="Event_AX"  runat ="server"   />     
None.gif    
</ div >
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >


Page后台:

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
public   partial   class  Default_AX : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {   
InBlock.gif
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//③为代理添加需要执行的事件
InBlock.gif
        UC_AXzhz.AX += new EventHandler(Event_AX); 
ExpandedSubBlockEnd.gif    }

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

InBlock.gif
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/pfs1314/archive/2010/06/24/1764276.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值