为什么 (sender,e) => SomeAction() 适用于 winform,不适用于 asp.net
我有以下代码:
btnTest.Click += (sender,e) => SomeAction()
为什么此代码在 WinForms 中有效,而在 asp.net 中无效。在 asp.net 中,我必须执行以下操作:
btnTest.Click += new EventHandler(SomeAction);
两种情况下的目标框架都是 .net 4.0
I have the following code:
btnTest.Click += (sender,e) => SomeAction()
why does this code works in WinForms and not in asp.net. In asp.net I had to do the following:
btnTest.Click += new EventHandler(SomeAction);
target framework in both cases is .net 4.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否可能尝试
从 Page_Load 方法或另一个事件处理程序内部调用?在这种情况下,参数“sender”和“e”已经声明并且可能会导致冲突。
将定义更改为:
您可能希望将参数转发给您的函数:
Is it possible you are trying to call
from inside the Page_Load method or another event handler? In that case the parameters "sender" and "e" are already declared and can be causing a conflict.
Change the definition to:
You'll probably want to forward the arguments to your function though:
对我来说 ASP.NET 4.0 工作正常:
It works fine is ASP.NET 4.0 for me: