9月11日美国放假吗:object Source, EventArgs e 是什么吗啊

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/12 03:51:59
能给详细解释一下吗 ??


谢了
我也看不懂啊,哪位能不能不能说的一下明了啊,也就是说能简单解释,但是能有达到明白的感觉啊~~~~~~

object sender, EventArgs e

object Source, EventArgs e
有什么区别啊,不是字面上的,无聊人不要说话

说的通俗一些,就是:

有一个叫做EventHandler 的家伙,他会告诉你(主程序),有一些事情发生了:这个事情是谁导致的呢?是某个object类型对象导致的,它用Source或Sender来表示。这个事情是什么事呢?e的内容就是事情的内容了。

至于Source和Sender,没有区别,你想用哪个就用哪个,其实都是一样的。

所以,我们在程序中的事件处理函数就是依赖于这个东西实现的:比方说你点了一个按钮,程序怎么知道应该用哪个函数来处理这个动作呢?那么EventHandler 这个家伙会告诉程序:"button1(sender)被点击(e)了,请调用对应的处理函数"。当然这个函数是谁,这个函数要做什么,是由你自己写的。

再深入一层,这个过程实际上就是:你的动作被windows捕获,windows把这个动作作为系统消息发送给程序(可以看message结构),程序从自己的消息队列中不断的取出消息,并在消息循环中寻找对应的处理方式,这时message结构中的类似于sender和e的东东就起到了引导程序使用正确的处理函数的作用。

归根究底,这个sender和e及其一整套的处理方式,只不过是windows消息机制的另外一种表现罢了 ^_^

委托类型 EventHandler 在它的调用签名中定义了两个参数。
第一个为是基于通用 Object 类型的Source,EventHandler 定义的第二个参数名为 e
object sender, EventArgs e 同理

这是我从微软查来的,我是看不懂什么意思。
自己去研究研究把

ASP.NET 页框架提供一种称为“事件冒泡”的技术,允许子控件将事件沿其包容层次结构向上传播。事件冒泡允许在控件层次结构中更方便的位置引发事件,并且允许将事件处理程序附加到原始控件以及公开冒泡的事件的控件上。

数据绑定控件(Repeater、DataList 和 DataGrid)使用事件冒泡将子控件(在项目模板内)引发的命令事件公开为顶级事件。虽然 .NET Framework 中的 ASP.NET 服务器控件将事件冒泡用于命令事件(事件数据类是从 CommandEventArgs 派生的事件),但是,服务器控件上定义的任何事件都可以冒泡。

控件可以通过从基类 System.Web.UI.Control 继承的两个方法参与事件冒泡。这两个方法是:OnBubbleEvent 和 RaiseBubbleEvent。以下代码片段显示了这些方法的签名。

[C#]
protected virtual bool OnBubbleEvent(
object source,
EventArgs args
);
protected void RaiseBubbleEvent(
object source,
EventArgs args
);
[Visual Basic]
Overridable Protected Function OnBubbleEvent( _
ByVal source As Object, _
ByVal args As EventArgs _
) As Boolean
Protected Sub RaiseBubbleEvent( _
ByVal source As Object, _
ByVal args As EventArgs _
)
RaiseBubbleEvent 的实现是由 Control 提供的,并且不能被重写。RaiseBubbleEvent 沿层次结构向上将事件数据发送到控件的父级。若要处理或引发冒泡的事件,控件必须重写 OnBubbleEvent 方法。

使事件冒泡的控件执行以下三种操作之一。

控件不执行任何操作,此时事件自动向上冒泡到其父级。
控件进行一些处理并继续使事件冒泡。若要实现这一点,控件必须重写 OnBubbleEvent,并从 OnBubbleEvent 调用 RaiseBubbleEvent。以下代码片段(摘自模板化数据绑定控件示例)在检查事件参数的类型后使事件冒泡。
[C#]
protected override bool OnBubbleEvent(object source, EventArgs e) {
if (e is CommandEventArgs) {
// Adds information about an Item to the
// CommandEvent.
TemplatedListCommandEventArgs args =
new TemplatedListCommandEventArgs(this, source, (CommandEventArgs)e);
RaiseBubbleEvent(this, args);
return true;
}
return false;
}
[Visual Basic]
Protected Overrides Function OnBubbleEvent(source As Object, e As EventArgs) As Boolean
If TypeOf e Is CommandEventArgs Then
' Adds information about an Item to the
' CommandEvent.
Dim args As New TemplatedListCommandEventArgs(Me, source, CType(e, CommandEventArgs))
RaiseBubbleEvent(Me, args)
Return True
End If
Return False
End Function
控件停止事件冒泡并引发和/或处理该事件。引发事件需要调用将事件调度给侦听器的方法。若要引发冒泡的事件,控件必须重写 OnBubbleEvent 以调用引发此冒泡的事件的 OnEventName 方法。引发冒泡的事件的控件通常将冒泡的事件公开为顶级事件。以下代码片段(摘自模板化数据绑定控件示例)引发一个冒泡的事件。
[C#]
protected override bool OnBubbleEvent(object source, EventArgs e) {
bool handled = false;

if (e is TemplatedListCommandEventArgs) {
TemplatedListCommandEventArgs ce = (TemplatedListCommandEventArgs)e;

OnItemCommand(ce);
handled = true;
}
return handled;
}
[Visual Basic]
Protected Overrides Function OnBubbleEvent(source As Object, e As EventArgs) As Boolean
Dim handled As Boolean = False

If TypeOf e Is TemplatedListCommandEventArgs Then
Dim ce As TemplatedListCommandEventArgs = CType(e, TemplatedListCommandEventArgs)

OnItemCommand(ce)
handled = True
End If
Return handled
End Function
有关说明事件冒泡的示例,请参见事件冒泡控件示例和模板化数据绑定控件示例。

注意 虽然启用事件冒泡的方法 OnBubbleEvent 符合用于引发事件的方法的标准 .NET Framework 命名模式,但是没有名为 BubbleEvent 的事件。在停止事件冒泡的控件中,将冒泡事件公开为顶级事件。例如,DataList 控件将其模板中控件的 Command 事件公开为 ItemCommand 事件。另请注意,在 .NET Framework 中 OnEventName 方法的标准签名有一个参数 (protected void OnEventName (EventArgs e))。但是,OnBubbleEvent 有两个参数,这是因为该事件起源于控件之外;第二个参数提供源。
到目前为止,本讨论说明了控件如何响应冒泡的事件。下面一节显示如何创作一个定义冒泡的事件的控件。

定义冒泡的事件
如果希望控件为它所定义的事件启用事件冒泡,则控件必须从引发该事件的 OnEventName 方法调用 RaiseBubbleEvent。不需要在该控件中做额外的工作。以下代码片段显示了一个控件,该控件定义了一个启用冒泡的 Command 事件。

[C#]
protected virtual void OnCommand(CommandEventArgs e) {
CommandEventHandler handler = (CommandEventHandler)Events[EventCommand];
if (handler != null)
handler(this,e);

// The Command event is bubbled up the control hierarchy.
RaiseBubbleEvent(this, e);
}
[Visual Basic]
Protected Overridable Sub OnCommand(e As CommandEventArgs)
Dim handler As CommandEventHandler = CType(Events(EventCommand), CommandEventHandler)
If Not (handler Is Nothing) Then
handler(Me, e)
End If
' The Command event is bubbled up the control hierarchy.
RaiseBubbleEvent(Me, e)
End Sub
注意 事件冒泡并不限于命令事件。可以使用此处描述的机制使任何事件冒泡。