.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?
基础教程推荐
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
