Asked by:
Custom Event Handler on a Extended Calendar Control

Question
-
User1851297965 posted
I hope this is the right place to post this. For the record i've tried google and searching but i can't find what im looking in regards to Custom Event Handlers.
I've extended the Calendar control to put event names into each day based on a couple of projects on codeproject. But what im trying to add now is an event handler to the event name so when i put it in the web page i can call the event when the event name is clicked and display the information about the event. But i'm lost to how i can create this. The only thing i can manage to do is add the javascript to the event name and call the SelectionChanged event. But i feel thats a hack job and i want to do it properly.
Can someone explain to me how to achieve what i want to do or lead me in the right direction. Thanks.
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Data;
6 using System.Text;
7 using System.Web;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10 11 namespace ExtendedControls
12 {
13 [ToolboxData("<{0}:EventCalendar runat=server></{0}:EventCalendar>")]
14 public class EventCalendar : Calendar
15 {
16 #region Constructor 17 18 public EventCalendar()
19 : base()
20 {
21 DayRender += new DayRenderEventHandler(EventCalendarDayRender);
22 }
23 24 #endregion
25 26 #region Properties 27 28 public DataTable EventSource
29 {
30 get {
31 32 DataTable s = ViewState["EventSource"] as DataTable;
33 return s == null ? null : s;
34 }
35 set
36 {
37 ViewState["EventSource"] = value;
38 }
39 }
40 41 public string EventStartDateColumnName
42 {
43 get 44 {
45 string s = ViewState["StartDateColumnName"] as string;
46 return s == null ? String.Empty : s;
47 }
48 set
49 {
50 ViewState["StartDateColumnName"] = value;
51 }
52 }
53 54 public string EventEndDateColumnName
55 {
56 get 57 {
58 string s = ViewState["EndDateColumnName"] as string;
59 return s == null ? String.Empty : s;
60 }
61 set 62 {
63 ViewState["EndDateColumnName"] = value;
64 }
65 }
66 67 public string EventHeaderColumnName
68 {
69 get 70 {
71 string s = ViewState["EventHeaderColumnName"] as string;
72 return s == null ? String.Empty : s;
73 }
74 set 75 {
76 ViewState["EventHeaderColumnName"] = value;
77 }
78 }
79 80 public string EventCssClass
81 {
82 get 83 {
84 string s = ViewState["EventCssClass"] as string;
85 return s == null ? String.Empty : s;
86 }
87 set 88 {
89 ViewState["EventCssClass"] = value;
90 }
91 }
92 93 #endregion
94 95 #region Methods 96 97 protected void EventCalendarDayRender(object sender, DayRenderEventArgs e)
98 {
99 CalendarDay d = ((DayRenderEventArgs)e).Day;
100 TableCell c = ((DayRenderEventArgs)e).Cell;
101
102 // If there is nothing to bind 103 if (this.EventSource == null)
104 {
105 return;
106 }
107 108 //Get the Event Source 109 DataTable table = this.EventSource;
110 111 //Loop through each row 112 foreach (DataRow row in table.Rows)
113 {
114 #region Check Required Columns 115 116 if (EventStartDateColumnName == string.Empty)
117 {
118 throw new ApplicationException("Must set EventCalendar's EventStartDateColumnName property when EventSource is specified");
119 }
120 if (EventEndDateColumnName == string.Empty)
121 {
122 throw new ApplicationException("Must set EventCalendar's EventEndDateColumnName property when EventSource is specified");
123 }
124 if (EventHeaderColumnName == string.Empty)
125 {
126 throw new ApplicationException("Must set EventCalendar's EventHeaderColumnName property when EventSource is specified");
127 }
128 129 #endregion 130 131 DateTime startDate = Convert.ToDateTime(row[this.EventStartDateColumnName]);
132 DateTime endDate = Convert.ToDateTime(row[this.EventEndDateColumnName]);
133 string cssClass = string.Empty;
134 135 if (EventCssClass != null && row[EventCssClass] != null && row[EventCssClass].ToString() != "")
136 {
137 cssClass = row[EventCssClass].ToString();
138 }
139 140 if (!d.IsOtherMonth && d.Date >= startDate.Date && d.Date <= endDate.Date)
141 {
142 HyperLink hpl = new HyperLink();
143 Panel pnl = new Panel();
144
145 // Show the Event Text 146 hpl.Text = row[EventHeaderColumnName].ToString();
147 hpl.NavigateUrl = string.Format("javascript:__doPostBack('Calendar1','{0}')", row["Id"]);
148 149 pnl.Controls.Add(hpl);
150 151 c.Controls.Add(pnl);
152 }
153 }
154 }
155 156 #endregion 157 }
158 }
159Monday, August 25, 2008 7:57 PM
All replies
-
User1851297965 posted
I've added the stuff i highlighted in red. Which fire's the event and it has the right Command Name and Arguement but it fire's on page load. I can't get it to fire when the button is clicked....
1 #region Methods 2 3 protected void EventCalendarDayRender(object sender, DayRenderEventArgs e)
4 {
5 CalendarDay d = ((DayRenderEventArgs)e).Day;
6 TableCell c = ((DayRenderEventArgs)e).Cell;
7
8 // If there is nothing to bind 9 if (this.EventSource == null)
10 {
11 return;
12 }
13 14 //Get the Event Source 15 DataTable table = this.EventSource;
16 17 //Loop through each row 18 foreach (DataRow row in table.Rows)
19 {
20 #region Check Required Columns 21 22 if (EventStartDateColumnName == string.Empty)
23 {
24 throw new ApplicationException("Must set EventCalendar's EventStartDateColumnName property when EventSource is specified");
25 }
26 if (EventEndDateColumnName == string.Empty)
27 {
28 throw new ApplicationException("Must set EventCalendar's EventEndDateColumnName property when EventSource is specified");
29 }
30 if (EventHeaderColumnName == string.Empty)
31 {
32 throw new ApplicationException("Must set EventCalendar's EventHeaderColumnName property when EventSource is specified");
33 }
34 35 #endregion 36 37 DateTime startDate = Convert.ToDateTime(row[this.EventStartDateColumnName]);
38 DateTime endDate = Convert.ToDateTime(row[this.EventEndDateColumnName]);
39 string cssClass = string.Empty;
40 41 if (EventCssClass != null && row[EventCssClass] != null && row[EventCssClass].ToString() != "")
42 {
43 cssClass = row[EventCssClass].ToString();
44 }
45 46 if (!d.IsOtherMonth && d.Date >= startDate.Date && d.Date <= endDate.Date)
47 {
48 Button lbtn = new Button();
49 Panel pnl = new Panel();
50
51 // Show the Event Text 52 lbtn.Text = row[EventHeaderColumnName].ToString();
53 //hpl.NavigateUrl = string.Format("javascript:__doPostBack('Calendar1','{0}')", row["Id"]);
54 55 //lbtn.CommandArgument = row["Id"].ToString();
56 //lbtn.CommandName = "EventClicked";
57 //lbtn.Command += new CommandEventHandler(lbtn_Command); 58 59 CommandEventArgs args = new CommandEventArgs("EventClicked", row["Id"].ToString());
60 OnEventClicked(args);
61 62 pnl.Controls.Add(lbtn);
63 64 c.Controls.Add(pnl);
65 }
66 }
67 }
68 69 #endregion
70 71 72 73 protected static readonly object EventClick = new object();
74 75 public event CommandEventHandler EventClicked
76 {
77 add
78 {
79 base.Events.AddHandler(EventClick, value);
80 }
81 remove
82 {
83 base.Events.RemoveHandler(EventClick, value);
84 }
85 }
86 87 protected virtual void OnEventClicked(CommandEventArgs commandEventArgs)
88 {
89 CommandEventHandler eventHandler = (CommandEventHandler)Events[EventClick];
90 if (eventHandler != null)
91 {
92 eventHandler(this, commandEventArgs);
93 }
94 base.RaiseBubbleEvent(this, commandEventArgs);
95 }Monday, August 25, 2008 10:22 PM -
User1968518401 posted
Where is lbtn click event?
Tuesday, August 26, 2008 9:58 AM -
User1851297965 posted
It doesn't work so i deleted it.
I thought of adding the code in the button click event but none of the controls i add end up being server controls...
Like.. none of the controls have an id or naming container or anything. The output is like:
<td align="left" valign="top" style="height:100px;width:80px;">
<a href="javascript:__doPostBack('Calendar1','3163')" style="color:Black" title="August 29">29</a> <--- auto generated
<div>
<a href="javascript:__doPostBack('CalendarUpdatePanel','Event3')">My Three Days Event</a> <---- my div and link.
</div>
</td>The hack job i've done for now to continue working on this project is:
protected override void OnLoad(EventArgs e) { if (this.Page.Request.Form["__EVENTARGUMENT"] != null) { if (this.Page.Request.Form["__EVENTARGUMENT"].ToString().Contains("Event")) { string few = this.Page.Request.Form["__EVENTARGUMENT"].ToString(); string id = few.Remove(0, 5); CommandEventArgs args = new CommandEventArgs("EventClicked", id); OnEventClicked(args); } } base.OnLoad(e); } protected static readonly object EventClick = new object(); public event CommandEventHandler EventClicked { add { base.Events.AddHandler(EventClick, value); } remove { base.Events.RemoveHandler(EventClick, value); } } protected virtual void OnEventClicked(CommandEventArgs commandEventArgs) { CommandEventHandler eventHandler = (CommandEventHandler)Events[EventClick]; if (eventHandler != null) { eventHandler(this, commandEventArgs); } base.RaiseBubbleEvent(this, commandEventArgs); }
Tuesday, August 26, 2008 7:34 PM