1

I have a dynamically generated radio button with anonymous delegate declared as

in

private void SetFieldDependency(DocumentSimpleFieldDetailDto obj, Table table, RadioButton ctrlExtended, Panel pnl)
    {
        if (this.ListOfDependentFields != null)
        {
            var lstRelatedField = this.TemplateCompiledDto.LstSimpleFields.Where(a => a.FkDocumentTemplateSectionId == obj.FkDocumentTemplateSectionId
                                                                                && !a.IsGridField && a.FieldGroup == obj.FieldGroup);
            var objDependencyList = this.ListOfDependentFields.FindAll(h => lstRelatedField.Any(k => h.ParentFieldId == k.TemplateSimpleFieldDetailId));
            if (objDependencyList != null && objDependencyList.Count > 0)
            {
                ctrlExtended.AutoPostBack = true;
                ctrlExtended.CheckedChanged += (sender, e) =>
                                                   {
                                                       foreach (var fieldDependency in objDependencyList.Where(h => h.ChildFieldId != null))
                                                       {
                                                           var tr = table.FindControl(fieldDependency.ChildFieldId.ToString()) as TableRow;
                                                           if (tr != null)
                                                           {
                                                               if (fieldDependency.ParentFieldId == obj.TemplateSimpleFieldDetailId)
                                                               {
                                                                   tr.Visible = true;
                                                               }
                                                               else
                                                               {
                                                                   tr.Visible = !tr.Visible;
                                                               }                                                                   
                                                           }
                                                       }
                                                       SetGridFieldDependency(obj, pnl, objDependencyList);
                                                   };
            }
        }
    }

Now on some button submit event I have to call this radiobutton CheckChanged Event.If it is like ctrlExtended_Changed(object sender, EventArgs e)

I can easily call but how can i call binded anonymous delegate method?

Also I am using Asp.net

Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93

2 Answers2

1

if some other event need to triger this event CheckedChanged then you should definetly call CheckedChanged event. if you just want the logic in the anonymous delegate method to be called i suggest you place that logic in a method and call that method

edit

ctrlExtended.CheckedChanged += (sender, e) =>{
  // My logic
  SharedLogic(sender, e);
}
.
.
.
public void SharedLogic(object sender, EventHandler e)
{
// the shared logic
}

in that way when the event CheckedChanged fired your logic will still be called upon, but also the method SharedLogic will be called, and you can call that method from elsewhere

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
  • 1
    when people downvote you and don't leave a comment it suggest reputation hunter – No Idea For Name Aug 02 '13 at 08:31
  • I think you might haven't understand the question. The thing you mentioned – Kamran Shahid Aug 02 '13 at 09:35
  • 2
    let's see: you have an anonymous delegate method and you want to call it from a different location then just the event, am i close? – No Idea For Name Aug 02 '13 at 09:37
  • yeah exactly i just elaborated you answer more that it mate...you were right in the first place...:) – Shazhad Ilyas Aug 02 '13 at 09:38
  • I swear Shaz and "No Idea for Name" that i didn't downvote any of your answer dears – Kamran Shahid Aug 02 '13 at 09:44
  • in that case iam soory mate...just here to help :) – Shazhad Ilyas Aug 02 '13 at 09:44
  • No Problem Shaz :) I have provided more detail where i am registering anonymous method for the RadioButton. – Kamran Shahid Aug 02 '13 at 09:49
  • @KamranShahid i belive you. you have provided more code but still i don't know is my answer is good for you. am i right when i said "you have an anonymous delegate method and you want to call it from a different location then just the event" cus if so then my answer is valid – No Idea For Name Aug 02 '13 at 09:57
  • I obviously wanted the logic registered in the anonymous delegate method to be called. As I mentioned the parameters and objects called into the anonymous delegate method are initialized prior.How can i passed it into the method and call it. Kindly give the example considering my method – Kamran Shahid Aug 02 '13 at 10:09
  • @KamranShahid i've edited the answer, let me know if it's better now – No Idea For Name Aug 02 '13 at 10:41
  • I can also move the logic into the method.But method instead SharedLogic(sender, e); my method will become SharedLogic(DocumentSimpleFieldDetailDto obj, Table table, Panel pnl, List objDependencyList) as i needed the paremeter in those method so i can't provide object sender, EventHandler e in the parameter – Kamran Shahid Aug 02 '13 at 10:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34687/discussion-between-no-idea-for-name-and-kamran-shahid) – No Idea For Name Aug 02 '13 at 10:57
0

There are different way of using it..

public class NumberEventArgs : EventArgs
{
   private int _number;
   public NumberEventArgs(int num)
   {
      this._number = num;
   }
    public int getNumber
    {
      get
      {
        return _reached;
      }
}
// eventhandler Method
private void ShowMessage(object sender, NumberEventArgs e)
{
 MessageBox.Show("Hello user your ticket no is:" + e.getNumber().ToString());
}

 myRadioButton.CheckedChanged += (sender, e) =>{ ShowMessage(); }
 myRadioButton.CheckedChanged += (sender, e) => MessageBox.Show(string.Format("sender is: {0} and eventargumnet is:{1}",sender.getType(),e.toString()};
 myRadioButton.CheckedChanged += (sender, e) => string.Format("string = {0} and {1}", sender.getType() , e);
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
Shazhad Ilyas
  • 1,183
  • 8
  • 14
  • when people downvote you and don't leave a comment it suggest reputation hunter – Shazhad Ilyas Aug 02 '13 at 09:12
  • +1 to counter your -1 and because it's a good qnswer, if you be so kind and if you think my answer is good to counter my -1 – No Idea For Name Aug 02 '13 at 09:23
  • I have edited my questiona bit.I can't use the method as in the anonymous method i am using some paremeter and collection which i have used before registering the method – Kamran Shahid Aug 02 '13 at 09:37