有人可以解释这段代码吗?按钮btn =(按钮)发送器; [英] can someone explain this code? Button btn = (Button)sender;

查看:93
本文介绍了有人可以解释这段代码吗?按钮btn =(按钮)发送器;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者


Hii i''m beginner


protected void Button_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.Text = (Int32.Parse(btn.Text) + 1).ToString();


}

推荐答案

sender指调用触发事件处理程序的事件的对象.如果您有许多使用同一事件处理程序的对象,这将很有用.


在此处,应在单击按钮时调用"Button_Click"函数,因此您正在跟踪按钮单击事件.

//Button btn =(Button)sender;

由于按钮是发件人,因此您将创建一个具有相同按钮类的对象,并对该按钮对象进行类型转换并分配给本地对象,然后访问其属性.


如果您有许多使用同一事件处理程序的对象,这将很有用.
sender refers to the object that invoked the event that fired the event handler. This is useful if you have many objects using the same event handler.


Here the "Button_Click" function should be called on the click of a button , hence you are tracking the button click event.

//Button btn = (Button)sender;

as sender is the button , you are creating a object of the same button class and typecasting the button object and assigning to a local object, then accessing it''s properties.


This is useful if you have many objects using the same event handler.


这篇关于有人可以解释这段代码吗?按钮btn =(按钮)发送器;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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