从System.EventHandler到System.EventHandler的<C#动态模板隐式转换错误<teventargs> </teventargs>

时间:2010-01-15 18:53:06

标签: c# templates event-handling

代码:

    public void InstantiateIn(System.Web.UI.Control container)
    {
        PlaceHolder ph = new PlaceHolder();
        SectionArgs e = new SectionArgs();
        ph.DataBinding += new EventHandler<SectionArgs>(ItemTemplate_DataBinding);
        container.Controls.Add(ph);
    }

    static void ItemTemplate_DataBinding(object sender, SectionArgs e)
    {
        PlaceHolder ph = (PlaceHolder)sender;
    }

错误: 无法隐式转换类型'System.EventHandler&lt; UserControlLibrary.Section.ItemTemplate.SectionArgs&gt;'到'System.EventHandler'

1 个答案:

答案 0 :(得分:1)

正在收到错误,因为PlaceHolder.DataBindingEventHandler,而不是EventHandler<SectionArgs>,但您尝试使用错误的委托类型进行订阅。

这应该是:

public void InstantiateIn(System.Web.UI.Control container) 
{ 
    PlaceHolder ph = new PlaceHolder(); 
    SectionArgs e = new SectionArgs(); 
    ph.DataBinding += new EventHandler(ItemTemplate_DataBinding); 
    container.Controls.Add(ph); 
} 

static void ItemTemplate_DataBinding(object sender, EventArgs e) 
{ 
    PlaceHolder ph = (PlaceHolder)sender; 
} 

以上操作正常。