views:

117

answers:

2

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'

+1  A: 

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
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.
Michael Samples
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
A: 

hi Michael, what was the solution here?

andrew