why does (sender,e) => SomeAction() works on winforms and not in asp.net

假如想象 提交于 2019-12-24 19:01:43

问题


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


回答1:


Is it possible you are trying to call

btnTest.Click += (sender,e) => SomeAction() 

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:

btnTest.Click += (s,ea) => SomeAction();

You'll probably want to forward the arguments to your function though:

btnTest.Click += (s,ea) => SomeAction(s, ea);



回答2:


It works fine is ASP.NET 4.0 for me:

public partial class _Default : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
   }

   private void SomeFunc()
   {
      Button1.Click += (sender, e) => SomeAction();
   }

   private void SomeAction()
   {
   }
}


来源:https://stackoverflow.com/questions/4017666/why-does-sender-e-someaction-works-on-winforms-and-not-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!