代码:
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'
答案 0 :(得分:1)
正在收到错误,因为PlaceHolder.DataBinding是EventHandler
,而不是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;
}
以上操作正常。