.NET Events - What are object sender amp; EventArgs e?(.NET 事件 - 什么是对象发送者 amp;事件参数 e?)
问题描述
sender 和 eventArgs 是什么意思/指的是什么?我怎样才能使用它们(对于下面的场景)?
What do sender and eventArgs mean/refer to? How can I make use of them (for the scenario below)?
场景:
我正在尝试使用删除功能构建自定义控件,并且我希望能够删除在包含许多相同自定义控件的页面上单击的控件.
I'm trying to build a custom control with a delete function, and I want to be able to delete the control that was clicked on a page that contains many of the same custom control.
推荐答案
发送者是操作的控件(比如 OnClick,它是按钮).
The sender is the control that the action is for (say OnClick, it's the button).
EventArgs 是此事件的实现者可能会发现有用的参数.使用 OnClick 它不会包含任何好处,但在某些事件中,例如在 GridView 'SelectedIndexChanged' 中,它将包含新索引或其他一些有用的数据.
The EventArgs are arguments that the implementor of this event may find useful. With OnClick it contains nothing good, but in some events, like say in a GridView 'SelectedIndexChanged', it will contain the new index, or some other useful data.
克里斯的意思是你可以这样做:
What Chris is saying is you can do this:
protected void someButton_Click (object sender, EventArgs ea)
{
Button someButton = sender as Button;
if(someButton != null)
{
someButton.Text = "I was clicked!";
}
}
这篇关于.NET 事件 - 什么是对象发送者 &事件参数 e?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:.NET 事件 - 什么是对象发送者 &事件参数 e?


基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01