By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 2,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

problem in rendering the calendar control

P: n/a
Hi All,

I want to create custom control by using Web.UI.WebControls.Calendar,
in which I want to set few days with different color. I had created two
property to set the color and storing in viewstate. I had created 4
methods to Add/Remove my special dates in viewstate.

Up to here all seems perfect, but how do I used it in prerender event
for my new calendar control.

Scenario:
I want to creae a web applicaiton which for Even Organizer.
I want to create my own custom calendar control/ User control which
show the dates in different color. There are list of dates on which the
event is organized, and there are few dates which are expiered. I want
to display this dates in two different color. I can easily implement it
in OnDayRender(object sender, DayRenderEventArgs e) event of system
calendar control. But i want to use same functionality on multiple web
pages, and for that i want to create Custom Control/User Control.

My quesion is where should i put my code to change the color of dates.

Below is my stuff. Your ideas also most welcomed.
Thanks & Regards,
Rushikesh

/////////////*********** CODE FOR EventCalendarControl
******************////////////////

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace MACT.Controls
{
[ToolboxData("<{0}:EventCalendarControl
runat=server></{0}:EventCalendarControl>")]
public class EventCalendarControl :
System.Web.UI.WebControls.Calendar
{
[Bindable(true),
Category("Appearance"),
DefaultValue("")]

#region Properties
public Color EventColor
{
get
{
object o = ViewState["EventColor"];
if (o == null)
return Color.Empty;
else
return (Color)o;
}

set
{
ViewState["EventColor"] = value;
}
}

public Color ExpieredEventColor
{
get
{
object o = ViewState["ExpieredEventColor"];
if (o == null)
return Color.Empty;
else
return (Color)o;
}

set
{
ViewState["ExpieredEventColor"] = value;
}
}
#endregion

#region Add/Remove Date Functionality
public void AddEventDate(DateTime dt)
{
object o = ViewState["EventDates"];
ArrayList dtArray;
if (o == null)
dtArray = new ArrayList(10);
else
dtArray = (ArrayList)o;
dtArray.Add(dt);
ViewState["EventDates"] = dtArray;
}

public void AddExpierEventDate(DateTime dt)
{
object o = ViewState["ExpierEventDate"];

ArrayList dtArray;
if (o == null)
dtArray = new ArrayList(10);
else
dtArray = (ArrayList)o;

dtArray.Add(dt);
ViewState["ExpierEventDate"] = dtArray;
}

public void RemoveEventDate(DateTime dt)
{
object o = ViewState["EventDates"];
ArrayList dtArray;
if (o == null)
dtArray = new ArrayList(10);
else
dtArray = (ArrayList)o;
dtArray.Remove(dt);
ViewState["EventDates"] = dtArray;
}

public void RemoveExpierEventDate(DateTime dt)
{
object o = ViewState["ExpierEventDates"];
ArrayList dtArray;
if (o == null)
dtArray = new ArrayList(10);
else
dtArray = (ArrayList)o;
dtArray.Remove(dt);
ViewState["ExpierEventDates"] = dtArray;
}

private ArrayList EventDates
{
get
{
object o = ViewState["EventDates"];
ArrayList dtArray = (ArrayList)o;
return dtArray;
}
}

private ArrayList ExpierEventDates
{
get
{
object o = ViewState["ExpierEventDates"];
ArrayList dtArray = (ArrayList)o;
return dtArray;
}
}

#endregion Add/Remove Date Functionality
protected override void OnPreRender(object sender,
DayRenderEventArgs e)
{
//Set event day style
Style eventDateStyle = new Style();
eventDateStyle.BackColor = this.EventColor;
eventDateStyle.Font.Bold = true;

//Set expiered event day style
Style expieredEventDateStyle = new Style();
expieredEventDateStyle.BackColor = this.ExpieredEventColor;
expieredEventDateStyle.Font.Bold = true;

//Check for date is event day
if ((IsEventDay(e.Day.Date)))
{
e.Cell.ApplyStyle(eventDateStyle);
}
//Check for date is expiered event day
else if ((IsExpieredEventDay(e.Day.Date)))
{
e.Cell.ApplyStyle(expieredEventDateStyle);
}
}

private bool IsEventDay(DateTime dt)
{
foreach (DateTime dt1 in this.EventDates)
if ((dt1 == dt))
return true;

return false;
}

private bool IsExpieredEventDay(DateTime dt)
{
foreach (DateTime dt1 in this.ExpieredEventDates)
if ((dt1 == dt))
return true;

return false;
}

}

}

Mar 28 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,

You can override the OnDayRender handler rather than use OnPreRender
because you have easy access to the particular day that is being
rendered in DayRenderEventArgs.

Protected Overrides Sub OnDayRender(s As Object, e As
DayRenderEventArgs)

If IsEventDay(e.Day.Date) Then
'Apply styling to event.
e.Cell.Font.Bold = True
e.Cell.BackColor = Me.EventColor
Else If IsExpiredEventDay(e.Day.Date) Then
'Apply styling to expired event.
e.Cell.BackColor = Me.ExpiredEventColor
End If

End Sub

Good Luck.

Mar 28 '06 #2

P: n/a
Hi,

Thank you for ur quick response.
I agree with you.
But how do i override OnDayRender event. it's not a virtual method of
my base class that is web.ui.controls.calendar

I am getting following error.

EventCalendarControl.OnDayRender(object,
System.Web.UI.WebControls.DayRenderEventArgs)': no suitable method
found to override

Thanks & Regards,
Rushikesh

Ca**********@gmail.com wrote:
Hi,

You can override the OnDayRender handler rather than use OnPreRender
because you have easy access to the particular day that is being
rendered in DayRenderEventArgs.

Protected Overrides Sub OnDayRender(s As Object, e As
DayRenderEventArgs)

If IsEventDay(e.Day.Date) Then
'Apply styling to event.
e.Cell.Font.Bold = True
e.Cell.BackColor = Me.EventColor
Else If IsExpiredEventDay(e.Day.Date) Then
'Apply styling to expired event.
e.Cell.BackColor = Me.ExpiredEventColor
End If

End Sub

Good Luck.


Mar 28 '06 #3

P: n/a
Hi,

Thank you for ur quick response.
I agree with you.
But how do i override OnDayRender event. it's not a virtual method of
my base class that is web.ui.controls.calendar

I am getting following error.

EventCalendarControl.OnDayRender(object,
System.Web.UI.WebControls.DayRenderEventArgs)': no suitable method
found to override.

I think for that i have to use below code...

protected virtual void OnDayRender(TableCell cell, CalendarDay day)
{
if (IsDocumentedDay(day.Date))
{
cell.Font.Bold = true;
cell.BackColor = this.DocumentedColor;
}
else if(IsNotDocumentedDay(day.Date))
{
cell.Font.Bold = true;
cell.BackColor = this.NotDocumentedColor;
}
}

But question is when ll this OnDayRender event call.

this event is not calling in my custom control.

Thanks & Regards,
Rushikesh

Ca**********@gmail.com wrote:
Hi,

You can override the OnDayRender handler rather than use OnPreRender
because you have easy access to the particular day that is being
rendered in DayRenderEventArgs.

Protected Overrides Sub OnDayRender(s As Object, e As
DayRenderEventArgs)

If IsEventDay(e.Day.Date) Then
'Apply styling to event.
e.Cell.Font.Bold = True
e.Cell.BackColor = Me.EventColor
Else If IsExpiredEventDay(e.Day.Date) Then
'Apply styling to expired event.
e.Cell.BackColor = Me.ExpiredEventColor
End If

End Sub

Good Luck.


Mar 28 '06 #4

P: n/a
You're correct.
So you'll have to hook onto the DayRender event and do your
customisation within the handler.

Eg. Assuming you're subclassing calendar directly.

Public Sub New()
AddHandler Me.DayRender, AddressOf MyDayRender
End Sub

Private Sub MyDayRender(ByVal s As Object, ByVal e As
DayRenderEventArgs)
If e.Day.IsToday Then
e.Cell.BackColor = Drawing.Color.AliceBlue
End If
End Sub

Mar 28 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.