开发者

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

开发者 https://www.devze.com 2023-01-21 03:22 出处:网络
I have the fol开发者_StackOverflow中文版lowing code: btnTest.Click += (sender,e) => SomeAction()

I have the fol开发者_StackOverflow中文版lowing 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


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);


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()
   {
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消