1

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

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

4

2 回答 2

1

如果其他一些事件需要触发这个事件CheckedChanged,那么你应该明确地调用CheckedChangedevent. 如果您只想调用匿名委托方法中的逻辑,我建议您将该逻辑放在一个方法中并调用该方法

编辑

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

这样,当事件CheckedChanged触发时,您的逻辑仍然会被调用,而且 SharedLogic 方法也会被调用,您可以从其他地方调用该方法

于 2013-08-02T08:05:20.183 回答
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);
于 2013-08-02T08:50:13.373 回答