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.
精彩评论