C# pass additional parameter to an event handler while binding the event at the run time

I have a link button which have a regular click event :

protected void lnkSynEvent_Click(object sender, EventArgs e)
{
}

And I bind this event at the runtime :

lnkSynEvent.Click += new EventHandler(lnkSynEvent_Click);

Now I need the function to accept additional argument:

protected void lnkSynEvent_Click(object sender, EventArgs e, DataTable dataT)
{
}

And pass the same as parameter while binding this event :

lnkSynEvent.Click += new EventHandler(lnkSynEvent_Click, //somehow here);

Not sure how to achieve this. Please help.

Thanks in advance.

Vishal

Answers


You can use anonymous delegate for that:

lnkSynEvent.Click += 
         new EventHandler((s,e)=>lnkSynEvent_Click(s, e, your_parameter));

I don't know exactly when it's changed, but now it's even easier!

lnkSynEvent.Click += (s,e) => lnkSynEvent_Click(s, e, your_parameter);

by use of delegate:

lnkbtnDel.Click += delegate(object s, EventArgs e1) { 
                 Dynamic_Click(s, e1, lnkbtnDel.ID); 
               };`  

EventHandler myEvent = (sender, e) => MyMethod(myParameter);//my delegate

myButton.Click += myEvent;//suscribe
myButton.Click -= myEvent;//unsuscribe

private void MyMethod(MyParameterType myParameter)
{
 //Do something
}

Need Your Help

How to convert RGB from YUV420p for ffmpeg encoder?

ffmpeg rgb video-encoding yuv

I want to make .avi video file from bitmap images by using c++ code.

How to get Selected items from Multi Select List View

android android-listview

I am using an array adapter and to this am adding an array list of string s , the list is multi select , How can i get the values of list items clicked ?