.NET 事件 - 什么是对象发送者 &事件参数 e? [英] .NET Events - What are object sender & EventArgs e?

查看:17
本文介绍了.NET 事件 - 什么是对象发送者 &事件参数 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆