开发者

C# Dynamic template implicit conversion error from System.EventHandler to System.EventHandler<TEventArgs>

Code:

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

Error: Cannot implicitly convert type 'System.EventHandler<UserControlLibrary.Section.ItemTemplate.SectionArgs>' to 'System.EventHandler'


The error is being received because PlaceHolder.DataBinding is an EventHandler, not an EventHandler<SectionArgs>, but you're trying to subscribe with the wrong type of delegate.

This should be:

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

The above will work correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜