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!
Thanks again!
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!