[英]pass control ID to custom method on text changed event
I would like to write a single method to handle the text changed event of several server controls on a webform. 我想编写一个方法来处理webform上几个服务器控件的文本更改事件。 When the text changed event fires I need to pass the control ID to my method.
当文本更改事件触发时,我需要将控件ID传递给我的方法。 In other words, when my method is called, I have to figure out which control caused the text changed event to fire.
换句话说,当调用我的方法时,我必须弄清楚哪个控件导致文本更改事件触发。 Is this possible?
这可能吗? Normally I would use the command argument property but I don't think this is available for textbox and listbox controls.
通常我会使用命令参数属性,但我认为这不适用于文本框和列表框控件。 Any suggestions?
有什么建议么?
Use the Sender
(first) parameter in the TextChanged function: 使用TextChanged函数中的
Sender
(first)参数:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
if (tb != null)
{
string id = tb.ID;
}
}
your event handler has two parameters: object sender, eventargs e
您的事件处理程序有两个参数:
object sender, eventargs e
The sender parameter is what you're looking for. sender参数是您正在寻找的。 Its actually a reference to the object which called the event handler, you just have to cast it to the correct type.
它实际上是对调用事件处理程序的对象的引用,您只需将其强制转换为正确的类型即可。
TextBox fooBar = sender as TextBox;
then you can get the name if you desire: 如果你愿意,你可以得到这个名字:
if(fooBar.Name == "someName") { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.