调用radiobutton CheckedChanged的匿名绑定委托方法

时间:2013-08-02 08:00:47

标签: c# c#-4.0 event-handling anonymous-methods

我有一个动态生成的单选按钮,匿名委托声明为

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

现在,在某个按钮提交事件中,我必须调用此radiobutton CheckChanged个事件。如果它像ctrlExtended_Changed(object sender, EventArgs e)

我可以轻松打电话但是如何调用带绑定的匿名委托方法?

我也在使用Asp.net

2 个答案:

答案 0 :(得分:1)

如果某个其他事件需要对此事件进行分类CheckedChanged,那么您应该确定地调用CheckedChanged事件。如果您只是想要调用匿名委托方法中的逻辑,我建议您将该逻辑放在方法中并调用该方法

修改

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

以这种方式,当事件CheckedChanged解雇你的逻辑时仍然会被调用,但也会调用SharedLogic方法,你可以从其他地方调用该方法

答案 1 :(得分:0)

有不同的使用方法..

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