Search code examples
c#insertdatalistlinkbuttonevent-handling

Extending DataList to accept a new InsertCommand from LinkButtons


I'm trying to subclass DataList to accept a new Command from embedded LinkButtons. Here's my abstract class:

public abstract class BaseFieldGroup : DataList
{
    public const string InsertCommandName = "Insert";
    public event DataListCommandEventHandler InsertCommand
    {
        add
        {
            base.Events.AddHandler(EventInsertCommand, value);
        }
        remove
        {
            base.Events.RemoveHandler(EventInsertCommand, value);
        }
    }
    private static readonly object EventInsertCommand;
    static BaseFieldGroup()
    {
        EventInsertCommand = new object();
    }
    protected virtual void OnInsertCommand(DataListCommandEventArgs e)
    {
        DataListCommandEventHandler handler = (DataListCommandEventHandler)base.Events[EventInsertCommand];
        if (handler != null)
        {
            handler(this, e);
        }
    }
}

it seems right, but the Event isn't being caught; I'm not sure why. However, I also noticed that LinkButton sends the even up with a RaiseBubble, I don't know if that's an issue or not. Anyone have thoughts?

Oh, this is 2.0


Solution

  • Aha! Found and fixed; I have to override the OnBubbleEvent of the DataList to accomodate (call) the new command. See below:

    public abstract class BaseFieldGroup : DataList
    {
    ...
        protected override bool OnBubbleEvent(object source, EventArgs e)
        {
            bool flag = false;
            if (e is DataListCommandEventArgs)
            {
                DataListCommandEventArgs args = (DataListCommandEventArgs)e;
                this.OnItemCommand(args);
                flag = true;
                switch (args.CommandName)
                {
                    case SelectCommandName:
                        this.SelectedIndex = args.Item.ItemIndex;
                        this.OnSelectedIndexChanged(EventArgs.Empty);
                        return flag;
                    case EditCommandName:
                        this.OnEditCommand(args);
                        return flag;
                    case DeleteCommandName:
                        this.OnDeleteCommand(args);
                        return flag;
                    case UpdateCommandName:
                        this.OnUpdateCommand(args);
                        return flag;
                    case CancelCommandName:
                        this.OnCancelCommand(args);
                        return flag; //??
                    case InsertCommandName:
                        this.OnInsertCommand(args);
                        return flag;
                }
            }
            return flag;
        }