C# - Child Control Events

Associate
Joined
9 Dec 2008
Posts
2,341
Location
Somewhere!
Hi All,

I'm pretty new to C# and ASP.Net, so please forgive me if I use the incorrect terminology throughout the post - but also please correct me if I do so!

Long story short - I'm trying to build a calendar control. I've managed to get as far as drawing the calendar on the screen, with 2 buttons to scroll between the months. The problem is, the buttons won't actually fire their events.

Now, my understanding is that this is an issue with "event bubbling". The page knows the buttons are there, as they are part of the custom control I've put together - but can't see the events they're attached too.

This is where I'm struggling to see what I'm missing... My code is as below, and any help, advice, or suggested reading would be greatly appreciated.

I've tried a few different variations of what I've got below based on things I've read on the internet, and I'm getting nowhere!

Code:
    [ToolboxData(@"<{0}:HolidayCalendar runat=""server"" \>")]
    public class HolidayCalendar : Calendar
    {
        private DateTime selectedDate = DateTime.Now;
        Button btnPreviousMonth = new Button();
        Button btnNextMonth = new Button();

        #region Properties and Delegates

        public DateTime SelectedDate
        {
            get { return selectedDate; }
            set { selectedDate = value; }
        }
        public delegate void PreviousMonthHandler(object sender, EventArgs e);
        public delegate void NextMonthHandler(object sender, EventArgs e);
        public event PreviousMonthHandler PreviousMonth;
        public event NextMonthHandler NextMonth;

        #endregion

        protected override void CreateChildControls()
        {
            btnPreviousMonth = new Button();
            btnPreviousMonth.Text = selectedDate.AddMonths(-1).ToString("MMMM");
            btnPreviousMonth.CommandName = "PreviousMonth";
            btnPreviousMonth.Click += new System.EventHandler(this.OnPreviousMonthChanged);
            //Controls.Add(btnPreviousMonth);

            btnNextMonth = new Button();
            btnNextMonth.Text = selectedDate.AddMonths(1).ToString("MMMM");
            btnNextMonth.CommandName = "NextMonth";
            btnNextMonth.Click += new System.EventHandler(this.OnNextMonthChanged);
            //Controls.Add(btnNextMonth);
        }

        protected virtual void OnPreviousMonth(EventArgs e)
        {
            if (PreviousMonth != null)
            {
                PreviousMonth(this, e);
                //selectedDate.AddMonths(-1);
            }
        }

        protected virtual void OnNextMonth(EventArgs e)
        {
            if (NextMonth != null)
            {
                NextMonth(this, e);
                //selectedDate.AddMonths(1);
            }
        }

        public HolidayCalendar()
        {
            this.Init += new EventHandler(HolidayCalendar_Init);
        }

        private void HolidayCalendar_Init(object sender, EventArgs e)
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.btnPreviousMonth.Click += new System.EventHandler(this.OnPreviousMonthChanged);
            this.btnNextMonth.Click += new System.EventHandler(this.OnNextMonthChanged);
        }

        private void OnPreviousMonthChanged(object sender, System.EventArgs e)
        {
           OnPreviousMonth(e);
       }
        private void OnNextMonthChanged(object sender, System.EventArgs e)
        {
            OnNextMonth(e);
        }
}

Thanks again!
 
Are you actually handling

Code:
        public event PreviousMonthHandler PreviousMonth;
        public event NextMonthHandler NextMonth;

anywhere? Like object.PreviousMonthHandler += whatever?
 
Is that not what the below does?

Code:
this.btnPreviousMonth.Click += new System.EventHandler(this.OnPreviousMonthChanged);
this.btnNextMonth.Click += new System.EventHandler(this.OnNextMonthChanged);
 
No, you need to put code in an event handler for it to do anything.
Code:
private void btnPreviousMonth_Click(object sender, EventArgs e) { 
    //do stuff here
}

Just make sure to wire the buttons click method to that. Which would be something like:
Code:
this.btnPreviousMonth.Click += new System.EventHandler(this.btnPreviousMonth_Click)

I probably didn't explain it very well since I only program my free time, someone else will be able to explain it better no doubt.
 
Last edited:
Well what you have done is you have handled the button clicks, all well and good, however you then go on to fire a second set of delegates. Do you actually have handlers in place for these?

Code:
        protected virtual void OnPreviousMonth(EventArgs e)
        {
            if (PreviousMonth != null)
            {
                PreviousMonth(this, e);
                //selectedDate.AddMonths(-1);
            }
        }

        protected virtual void OnNextMonth(EventArgs e)
        {
            if (NextMonth != null)
            {
                NextMonth(this, e);
                //selectedDate.AddMonths(1);
            }
        }

So that bit there won't actually do anything unless anything external to your control hooks up to the events like:

Code:
MyHolidayCalendar.PreviousMonth += PreviousMonthEventHandler;
MyHolidayCalendar.NextMonth += NextMonthEventHandler;
 
No, you need to put code in an event handler for it to do anything.
Code:
private void btnPreviousMonth_Click(object sender, EventArgs e) { 
    //do stuff here
}

Just make sure to wire the buttons click method to that. Which would be something like:
Code:
this.btnPreviousMonth.Click += new System.EventHandler(this.btnPreviousMonth_Click)

I probably didn't explain it very well since I only program my free time, someone else will be able to explain it better no doubt.

Well what you have done is you have handled the button clicks, all well and good, however you then go on to fire a second set of delegates. Do you actually have handlers in place for these?

Code:
        protected virtual void OnPreviousMonth(EventArgs e)
        {
            if (PreviousMonth != null)
            {
                PreviousMonth(this, e);
                //selectedDate.AddMonths(-1);
            }
        }

        protected virtual void OnNextMonth(EventArgs e)
        {
            if (NextMonth != null)
            {
                NextMonth(this, e);
                //selectedDate.AddMonths(1);
            }
        }

So that bit there won't actually do anything unless anything external to your control hooks up to the events like:

Code:
MyHolidayCalendar.PreviousMonth += PreviousMonthEventHandler;
MyHolidayCalendar.NextMonth += NextMonthEventHandler;

I see - Thanks both... I'll have a crack at doing this a little later!
 
Back
Top Bottom