0

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'

BFree
  • 102,548
  • 21
  • 159
  • 201

1 Answers1

1

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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • The problem is I cannot pass my custom event args that way. Which is apparently the only way to pass the data. SectionArgs is necessary since they contain my data. The above is a simplified version of what my code actualy is. –  Jan 15 '10 at 20:46
  • So if you know that `e` is always `SectionArgs` then you can just cast it in the event handler like so: `SectionArgs sectionArgs = e as SectionArgs;` – Igor Zevaka Aug 29 '10 at 23:18