Solved

using c# to manipulate the look of the calendar object

Posted on 2008-10-27
12
481 Views
Last Modified: 2012-05-05
Hi,

I am trying to get a calendar to display a different background for dates that have already passed.
I'm trying to use the dayrendered function,  getting the datetimenow and seeing if the date in the calendar cell is lesser than this date and if it is applying a different background colour to it.

My attempted code is as follows but I get the error:

CS0019: Operator '>' cannot be applied to operands of type 'int' and 'string'  

for the line : if (dblDay > e.Cell.Text)

So can I not do the comparing of the e.cell.text and todays day in this way or have i defined one of the variables incorrectly?

Many Thanks

protected void Page_Load(object sender, EventArgs e)

    {

        hidden.Visible = false;
 

        if (!IsPostBack)

        {

            OpenConnection();
 

            SqlCommand command = new SqlCommand("exec usp_GetValidMonths", conn);
 

            SqlDataReader reader = command.ExecuteReader();
 

            ArrayList yearList = new ArrayList();
 

            while (reader.Read())

            {

                string[] monthYear = SplitMonthYear(reader["month"].ToString());
 

                if (!yearList.Contains(monthYear[1]))

                    yearList.Add(monthYear[1]);

            }
 

            yearList.Sort();
 

            for (int i = 0; i < yearList.Count; i++)

            {

                ListItem item1 = new ListItem();

                item1.Text = (string)"20" + yearList[i];

                item1.Value = (string)yearList[i];

                year.Items.Add(item1);

            }
 
 

            CloseConnection();

        }
 

        if (!IsPostBack)

        {

            DateTimeFormatInfo dfi = new DateTimeFormatInfo();

            dfi.MonthDayPattern = "dd MMMM yy";
 

            DateTime now = DateTime.Now;
 

            string dateNow = now.ToString("m", dfi);
 

            string strDay = dateNow.Split(' ')[0];

            string strMonth = dateNow.Split(' ')[1];

            string strYear = dateNow.Split(' ')[2];
 

            string dateID = strMonth + " " + strYear;

            string todayDate = strDay;
 

            DisplayMonth(dateID , todayDate);
 

            dateDrop.Text = strDay;

            monthdrop.Text = strMonth;

            year.Text = strYear;
 

            DateTime datenowforcalendar = DateTime.Parse(dateNow);
 

            Calendar1.SelectedDate = datenowforcalendar;

            Calendar1.VisibleDate = datenowforcalendar;
 

        }

    }
 
 
 

  private void calEvents_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)

    {

        DateTimeFormatInfo dformat = new DateTimeFormatInfo();

        dformat.MonthDayPattern = "dd MMMM yy";
 

        DateTime datetoday = DateTime.Now;
 

        string todaysdate = datetoday.ToString("m", dformat);
 

        string strgDay = todaysdate.Split(' ')[0];
 

        int dblDay = Int32.Parse(strgDay);
 

        int celltext = Int32.Parse(e.Cell.Text);
 

        Style style = new Style();
 

        if (dblDay > e.Cell.Text)

        {

            style.BackColor = System.Drawing.ColorTranslator.FromHtml("#666666");
 

        }
 

    }

Open in new window

0
Comment
Question by:CharlieDev
  • 7
  • 4
12 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 150 total points
ID: 22811742
you should convert to integer before comparing a text:

 int cellvalue;
 if (int.TryParse(e.Cell.Text, out cellvalue) && dblDay > cellvalue)
{
    // etc
}
0
 

Author Comment

by:CharlieDev
ID: 22811890
I think that is working, but I'm not getting any background colour change

Can you see what might be wrong with my code :

 style.BackColor = System.Drawing.ColorTranslator.FromHtml("#666666");


I did just pinch it from an example i found on here
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22813218
because you have declared a style but you have never applied it to the day cell
do it something like this
e.Cell.BackColor = System.Drawing.Color.Wheat;
or
e.Cell.BackColor = System.Drawing.ColorTranslator.FromHtml("#666666");
0
 

Author Comment

by:CharlieDev
ID: 22813322
Thanks, I'm not managing to get it to work. I am wondering if I am not calling the function.
Should the private void calEvents_DayRender just automatically run do you know??
Cheers
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22813431
the event should be fired and the function should be called when a day is being rendered in the calendar control.
put a break point inside the day render event and see if the break point is being hit or not
also please confirm that your calendar control has registered to the dayrender event
0
 

Author Comment

by:CharlieDev
ID: 22813651
It seems that the dayrender event is not being executed. I am unsure how to tell with a breakpoint, I inserted one into the third line from the bottom of the dayrender event and then hovered over some of the above variables and they havent been filled, also i put a response.write at the very begining and this has not been displayed on the browser display aspx page.
I am now lost as to how to see if the calendar control has registered to the dayrender event.

Thanks for the help :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22814024
in your asp.net code do you have OnDayRender="calEvents_DayRender"
0
 

Author Comment

by:CharlieDev
ID: 22814541
I didnt have that reference in the asp calendar,ops.
I've put it in and i get this error:

CS0122: 'calendar_fixtcalendar.calEvents_DayRender(object, System.Web.UI.WebControls.DayRenderEventArgs)' is inaccessible due to its protection level

code line:   <asp:Calendar id="Calendar1"



which is from my aspx page code below

What does that mean?
Thank You
 <asp:Calendar id="Calendar1" 

                     BorderStyle="Double" 

                     BorderWidth="4px" 

                     BorderColor="green" 

                     DayStyle-BorderStyle="Solid" 

                     DayStyle-BorderWidth="1px"

                     OnSelectionChanged="Date_Selected"

                     OnDayRender="calEvents_DayRender"

                     width="429px"

                     height="280px"

                     Font-Name="Verdana" 

                     Font-Size="16px"

                     padding-left="5px"

                     NextPrevFormat="ShortMonth"

                     border-style="none"

                     text-decoration="none"

                     runat="server">

                   <TodayDayStyle />

                   <DayHeaderStyle Font-Bold="True"/>

                   <OtherMonthDayStyle ForeColor="gray"/>

                   <TitleStyle BackColor="#ffffff"

                               ForeColor="#000"

                               Font-Bold="True"/>
 

                   <SelectedDayStyle BackColor="#336633"

                                     Font-Bold="True"

                                     />

                   <NextPrevStyle ForeColor="#000"

                                  Font-Size="12px"/>

                   <SelectorStyle BackColor="#336633" 

                                  ForeColor="navy"

                                  Font-Size="9px"/>

                </asp:Calendar>

    <asp:Label ID="Label1" runat="server" ></asp:Label>

                    </div>

Open in new window

0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 350 total points
ID: 22814665
make your event handler protected
protected  void calEvents_DayRender
0
 

Author Comment

by:CharlieDev
ID: 22820119
Thanks, its running that code now
0
 

Author Comment

by:CharlieDev
ID: 22820123
Problem solved, for anyone that might want the code below is the working c# code
protected void calEvents_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)

    {

        DateTimeFormatInfo dformat = new DateTimeFormatInfo();

        dformat.MonthDayPattern = "dd M";
 

        DateTime datetoday = DateTime.Now;
 

        string todaysdate = datetoday.ToString("m", dformat);
 

        string strgDay = todaysdate.Split(' ')[0];

        string strgMonth = todaysdate.Split(' ')[1];
 

        int dblDay = Int32.Parse(strgDay);

        int dblMonth = Int32.Parse(strgMonth);
 

        int celltext = Int32.Parse(e.Day.DayNumberText);

        int cellMonth = e.Day.Date.Month;
 

        

        if ((dblDay > celltext && dblMonth == cellMonth) || dblMonth > cellMonth)

        {
 

            e.Cell.BackColor = System.Drawing.ColorTranslator.FromHtml("#eeeeee");

            e.Cell.ForeColor = System.Drawing.ColorTranslator.FromHtml("#999999");

        }
 

    }

Open in new window

0
 

Author Closing Comment

by:CharlieDev
ID: 31510249
Thanks again for your time, help and knowledge
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
In C#, how would you convert a byte array to its integer value equivalent? 12 38
Programmatically signing Word macros 4 61
Linq Help 1 35
DateTimepicker 4 33
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now