.NET事件 - 什么是对象发件人& EventArgs e? [英] .NET Events - What are object sender & EventArgs e?
问题描述
发件人和eventArgs是什么意思/引用?如何使用它们(对于下面的情况)?
What do sender and eventArgs mean/refer to? How can I make use of them (for the scenario below)?
场景:
我正在尝试使用delete函数构建一个自定义控件,并且我想要删除在包含许多相同的自定义控件的页面上点击的控件。
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它没有什么好的,但在某些事件中,就像在GridViewSelectedIndexChanged中说的那样,它将包含新的索引或其他有用的数据。
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事件 - 什么是对象发件人& EventArgs e?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!