tags:

views:

105

answers:

1

hi folks! i tryed to create an asp.net listbox that has a click evet. to do that i created a project that creates a dll file which i added to my toolbox in visual studio 2008. it does not work!!!! here is the code that creates the dll file:

namespace list_box
{
    public class list_box : ListBox, IButtonControl
    {
        private bool blCausesValidation = true;
        private string strCommandArgument = "";
        private string strCommandName = "";
        private string strPostBackUrl = "";
        private string strText = "";
        private string strValidationGroup = "";

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.CssClass = "frmbtn";
        }

        bool IButtonControl.CausesValidation
        {
            get
            {
                return blCausesValidation;
            }
            set
            {
                blCausesValidation = value;
            }
        }

        string IButtonControl.CommandArgument
        {
            get
            {
                return strCommandArgument;
            }
            set
            {
                strCommandArgument = value;
            }
        }

        string IButtonControl.CommandName
        {
            get
            {
                return strCommandName;
            }
            set
            {
                strCommandName = value;
            }
        }

        string IButtonControl.PostBackUrl
        {
            get
            {
                return strPostBackUrl;
            }
            set
            {
                strPostBackUrl = value;
            }
        }

        string IButtonControl.Text
        {
            get
            {
                return strText;
            }
            set
            {
                strText = value;
            }
        }

        string IButtonControl.ValidationGroup
        {
            get
            {
                return strValidationGroup;
            }
            set
            {
                strValidationGroup = value;
            }
        }


        public event EventHandler Click;

        void click_i(object sender, EventArgs e)
        {
            OnClick(new EventArgs());
        }

        protected virtual void OnClick(EventArgs e)
        {
            if (Click != null)
            {
                click_i(this, e);
            }
        }

        public event CommandEventHandler Command;

        void Command_i(object sender, CommandEventArgs e)
        {
            OnCommand(new CommandEventArgs(e));
        }

        protected virtual void OnCommand(CommandEventArgs e)
        {
            if (Command != null)
            {
                Command_i(this, e);
            }
        }
    }
}

help me!!!! i'm new at asp.net so....

A: 

Two things you should know regarding your code:

  1. In order for this to work, you must call OnClick from somewhere within your control (whenever you would like your event to fire).

  2. You have coded circular references in both your events.

Change this:

protected virtual void OnClick(EventArgs e)
{
    if (Click != null)
    {
        click_i(this, e);
    }
}

to this:

protected virtual void OnClick(EventArgs e)
{
    if (Click != null)
    {
        click(this, e);
    }
}

and remove this:

void click_i(object sender, EventArgs e)

Then all you have to do is add this to your control (again, wherever you want the event to fire:

this.OnClick(new EventArgs());
Gabriel McAdams