problem in rendering the calendar control

Discussion in 'ASP .Net Web Controls' started by rushikesh.joshi@gmail.com, Mar 28, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Guest

    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, 2006
    #2
    1. Advertising

  3. Guest

    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

    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, 2006
    #3
  4. Guest

    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

    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, 2006
    #4
  5. Guest

    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, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shevek
    Replies:
    3
    Views:
    6,096
    Shevek
    Jun 23, 2004
  2. Replies:
    4
    Views:
    1,093
  3. Peter Mount
    Replies:
    4
    Views:
    983
    Peter Mount
    Jan 31, 2006
  4. Shevek
    Replies:
    0
    Views:
    411
    Shevek
    Jun 23, 2004
  5. Robin9876

    Calendar Control not rendering correctly

    Robin9876, Aug 1, 2007, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    129
    Robin9876
    Aug 1, 2007
Loading...

Share This Page