直播中
namespace Michael.Communication.UserControls.Delegates
{
using EvengArgObjects;
// 聲明delegate(注意delegate其實是個對象),在C#中一般是(object sender, EventArgs e),sender是引發(fā)事件的對象的例示
//比如說在一個user control中有一個button引發(fā)了一個事件,那button就是sender,而不是user control
//delegate既可以在class之外聲明也可以在class之內聲明,一般在class之外聲明
public delegate void HeaderEventHandler(object sender, MichaelImageEventArgs e);
}
namespace Michael.Communication.UserControls.Delegates.EventArgObjects
{
// 繼承System.EventArgs,當單擊圖形按鈕時提供信息
public class MichaelImageEventArgs : System.EventArgs
{
private string imageTextClicked = null;
public string imageText
{
get {return imageTextClicked;}
set {imageTextClicked = value.Trim();}
}
}
}
注:上面的代碼實現(xiàn)了我們自己的Delegate和EventArgs
接下來我們寫一個User Control,包含六個Image Button
//Header.ascx
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="Header.ascx.cs" Inherits="Michael.Communication.UserControls.Header"%>
<asp:Table Runat="server" CellSpacing="0" CellPadding="0" id="Table1">
<asp:TableRow Runat="server">
<asp:TableCell Runat="Server">
<asp:Table Border="0" CellSpacing="0" CellPadding="0">
<asp:TableRow ID="row1" Runat="server">
<asp:TableCell Runat="server" ColumnSpan="2" HorizontalAlign="Left">
<asp:ImageButton ID="imgNewLogo" Runat="server" ImageUrl="images/newlogo.gif" OnClick="Image_Click" />
</asp:TableCell>
<asp:TableCell Runat="server" ColumnSpan="4" HorizontalAlign="Right">
<asp:ImageButton ID="imgNewHead" Runat="server" ImageUrl="images/newhead.gif" OnClick="Image_Click" />
</asp:TableCell>
</asp:TableRow>
<asp:Table>
<asp:TableRow ID="row2" Runat="server">
<asp:TableCell Runat="server">
<asp:ImageButton ID="imgBanner1" Runat="server" ImageUrl="images/banner1.gif" OnClick="Image_Click" />
</asp:TableCell>
<asp:TableCell Runat="server">
<asp:ImageButton ID="imgBanner2" Runat="server" ImageUrl="images/banner2.gif" OnClick="Image_Click" />
</asp:TableCell>
<asp:TableCell Runat="server">
<asp:ImageButton ID="imgBanner3" Runat="server" ImageUrl="images/banner3.gif" OnClick="Image_Click" />
</asp:TableCell>
<asp:TableCell Runat="server">
<asp:ImageButton ID="imgBanner4" Runat="server" ImageUrl="images/banner4.gif" OnClick="Image_Click" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
//Header.ascx.cs
namespace Michael.Communication.UserControls
{
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Michael.Communication.UserControls.Delegates;
using Michael.Communication.UserControls.Delegates.EventArgObjects;
public abstract class Header : System.Web.UI.UserControl
{
// 聲明事件(觸發(fā)事件相當于回調)
public event HeaderEventHandler headerEventHandler;
public void Image_Click(object sender, ImageClickEventArgs e)
{
if(headerEventHandler != null)
{
ImageButton ib = (ImageButton)sender;
// 創(chuàng)建EvenArgs對象的實例
MichaelImageEventArgs michaelImageEventArgs = new MichaelImageEventArgs();
michaelImageEventArgs.ImageTextClicked = GetImageButtonText(ib.ID, e);
foreach(headerEventHandler h in headerEventHandler.GetInvocationList())
{
try
{
// 事件觸發(fā)
h(ib, michaelImageEventArgs);
}
catch (Exception er)
{
// 處理意外
}
}
}
return;
}
//用ImageButton的ImageClickEventArgs返回X,Y確定點擊到圖片的那一部分
private string GetImageButtonText(string ID, ImageClickEventArgs e)
{
string returnValue = null;
if(ID == "imgNewLogo")
returnValue = "IBM";
if(ID == "imgNewHead")
{
if((e.Y > 0) && (e.Y < 35))
returnValue = "163.com";
else
returnValue = "Microsoft";
}
if(ID == "imgBanner1")
returnValue = "8488";
if(ID == "imgBanner2" || ID == "imgBanner3")
{
if((e.Y > 0) && (e.Y < 21))
returnValue = "chinabyte";
if((e.Y > 20) && (e.Y < 36))
returnValue = "www";
if(e.Y > 35)
returnValue = (ID == "imgBanner2" ? "sina.com.cn" : "5460.net");
if(e.X > 160)
returnValue = (ID == "imgBanner2" ? "my name is michael" : "what is your name");
}
if(ID == "imgBanner4")
{
if((e.Y > 0) && (e.Y < 36))
returnValue = "@163.com";
if(e.Y > 35)
returnValue = "www.163.com";
}
return returnValue;
}
}
}
主頁面
//Default.aspx
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="Michael.Communication.Default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD><TITLE>事件例子</TITLE></HEAD>
<BODY>
<FORM runat="server">
<asp:Table ID="tblLayout" Runat="server" CellSpacing="0" CellPadding="0">
<asp:TableRow ID="row1" Runat="server">
<asp:TableCell ID="cell11" Runat="server"></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="row2" Runat="server">
<asp:TableCell ID="cellTest1" Runat="server"
Text="你選擇的圖片所對應的文字是:(第一次事件被觸發(fā))"
Font-Name="Verdana" Font-Size="11pt" BackColor="Gainsboro" BorderStyle="Solid"
BorderWidth="1px" BorderColor="Black" HorizontalAlign="Center" />
</asp:TableRow>
<asp:TableCell ID="cellTest2" Runat="server"
Text="你選擇的圖片所對應的文字是:(第二次事件被觸發(fā))"
Font-Name="Verdana" Font-Size="11pt" BackColor="Gainsboro" BorderStyle="Solid"
BorderWidth="1px" BorderColor="Black" HorizontalAlign="Center" />
</asp:TableRow>
</asp:Table>
</FORM>
</BODY>
</HTML>
//default.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Michael.Communication.UserControls;
using Michael.Communication.UserControls.Delegates;
using Michael.Communication.UserControls.Delegates.EventArgObjects;
namespace Michael.Communication
{
public class Default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TableCell cell11;
protected System.Web.UI.WebControls.TableCell cellTest1;
protected System.Web.UI.WebControls.TableCell cellTest2;
public Default()
{
Page.Init += new System.EventHandler(Page_Init);
}
private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
private void Page_Load(object sender, System.EventArgs e)
{
cell11.Controls.Add(LoadHeader());
}
//觸發(fā)事件
public void SetText1(object sender, MichaelImageEventArgs e)
{
cellTest1.Text = "你選擇的圖片所對應的文字是: <B>{" + e.ImageText + "}</B>";
}
//觸發(fā)事件
public void SetText2(object sender, MichaelImageEventArgs e)
{
cellTest2.Text = "你選擇的圖片所對應的文字是: <B>{" + e.ImageText + "}</B>";
}
private Header LoadHeader()
{
Header header = (Header)Page.LoadControl("Header.ascx");
//事件關聯(lián)
header.HeaderEventHandler += new HeaderEventHandler(this.SetText1);
header.HeaderEventHandler += new HeaderEventHandler(this.SetText2);
return header;
}
}
}