delegate(委托)、event(事件) 的基础知识

1. event和delegate类型的字段到底有什么区别

 

http://www.cnblogs.com/Microshaoft/archive/2005/05/30/164753.html

 

2. 事件与事件处理程序

a)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace WebApplication14
{
public partial class _Default : System.Web.UI.Page
{
private static StringBuilder mysb = new StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{
HasEvent he
= new HasEvent();

he.SampleEvent
+= new HasEvent.MyEventHandler(SampleEventHandler);

he.DemoEvent(
"Hey there, Bruce!");
he.DemoEvent(
"How are you today?");
he.DemoEvent(
"I'm pretty good.");
he.DemoEvent(
"Thanks for asking!");
Label1.Text
= mysb.ToString() ;
}

private static void SampleEventHandler(object src, MyEventArgs mea)
{
mysb.Append(mea.Message);
}
}

public class MyEventArgs : EventArgs
{
private string msg;

public MyEventArgs(string messageData)
{
msg
= messageData;
}
public string Message
{
get { return msg; }
set { msg = value; }
}
}
//---------------------------------------------------------
public class HasEvent
{
// Declare an event of delegate type MyEventHandler of
// MyEventArgs.


public delegate void MyEventHandler(object sender, MyEventArgs e);
public event MyEventHandler SampleEvent;

public void DemoEvent(string val)
{
// Copy to a temporary variable to be thread-safe.

MyEventHandler temp
= SampleEvent;
if (temp != null)
temp(
this, new MyEventArgs(val));
}
}
}

 

b)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace WebApplication14
{
public partial class _Default : System.Web.UI.Page
{
private static StringBuilder mysb = new StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{
HasEvent he
= new HasEvent();
he.SampleEvent
+=
new EventHandler<MyEventArgs>(SampleEventHandler);

he.DemoEvent(
"Hey there, Bruce!");
he.DemoEvent(
"How are you today?");
he.DemoEvent(
"I'm pretty good.");
he.DemoEvent(
"Thanks for asking!");
Label1.Text
= mysb.ToString() ;
}

private static void SampleEventHandler(object src, MyEventArgs mea)
{
mysb.Append(mea.Message);
}
}

public class MyEventArgs : EventArgs
{
private string msg;

public MyEventArgs(string messageData)
{
msg
= messageData;
}
public string Message
{
get { return msg; }
set { msg = value; }
}
}
//---------------------------------------------------------
public class HasEvent
{
// Declare an event of delegate type EventHandler of
// MyEventArgs.

public event EventHandler<MyEventArgs> SampleEvent;

public void DemoEvent(string val)
{
// Copy to a temporary variable to be thread-safe.
EventHandler<MyEventArgs> temp = SampleEvent;
if (temp != null)
temp(
this, new MyEventArgs(val));
}
}
}

 

c) 假定用户控件(UserControl.ascx)中包含按钮控件Save,希望实现按Save按钮时,包含该用户控件的页面(Default.aspx)可以接收到事件。


UserControl.ascx.cs

public event EventHandler SaveClick;

protected void Save_Click(object sender, EventArgs e)
{
if (SaveClick != null)
SaveClick(
this, e);
}

 

Default.aspx.cs 代码

<uc1:WebUserControl1 ID="WebUserControl11" runat="server" OnSaveClick="Save_Click" />

protected void Save_Click(object sender, EventArgs e)
{
}

 

posted on 2010-12-06 16:26  小斌斌  阅读(451)  评论(0编辑  收藏  举报

导航