.NETGURU
Calendar Question
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngbeta' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Dan Hurwitz (VIP)
Hi All,

I am working with the Calendar control and have adapted some sample code
from the .Net Framework library. The adapted code is shown below.

If you run the aspx page, you will see there is a dropdownlist with the
months of the years, a label which shows the number of days selected, and a
button to select all the Fridays in the month.

The problem is that if you click the button, the label updates properly but
the selected days do not highlight. However, after using the dropdown to
select a new month, the button does correctly highlight the selected days.

If I uncomment the Page_Load method, then everything works correctly the
first time.

Why?

Thanks for any understanding you can shed.

--d

---------------------------------------
Calendar test.aspx
------------------

<%@ Page Language="C#" %>
<html>
<script runat="server">

    // Having this Page+Load makes the selected days visible first time
    // the button is clicked. Otherwise selected days not visible until
    // the second time the button is clicked.
//    void Page_Load(Object sender, EventArgs e)
//    {
//        cal.VisibleDate = cal.TodaysDate;
//    }

    void ddl_SelectedIndexChanged(Object sender, EventArgs e)
    {
        cal.SelectedDates.Clear();
        lblCountUpdate();
        cal.VisibleDate = new DateTime(cal.TodaysDate.Year,
Int32.Parse(ddl.SelectedItem.Value), 1);
    }

    void SelectionChanged(Object sender, EventArgs e)
    {
        lbl1.Text = "Today's Date is " + cal.TodaysDate.ToShortDateString();

        if (cal.SelectedDate != DateTime.MinValue)
            lbl2.Text = "The date selected is "+
cal.SelectedDate.ToShortDateString();

        lblCountUpdate();
    }

    void btnTgif_Click(Object sender, EventArgs e)
    {
        int currentDay = cal.VisibleDate.Day;
        int currentMonth = cal.VisibleDate.Month;
        int currentYear = cal.VisibleDate.Year;

        cal.SelectedDates.Clear();

        for (int i = 1; i <= System.DateTime.DaysInMonth(currentYear,
currentMonth); i++ )
        {
            DateTime date = new DateTime(currentYear, currentMonth, i);
            if (date.DayOfWeek == DayOfWeek.Friday)
                cal.SelectedDates.Add(date);
        }

        lblCountUpdate();
    }

    void lblCountUpdate()
    {
        lblCount.Text = "Count of Days Selected: " +
cal.SelectedDates.Count.ToString();
    }

</script>

    <body>
    <form runat="server">

        <asp:Calendar
            id="cal"
            SelectionMode="DayWeekMonth"
            ShowGridLines="true"
            onSelectionChanged="SelectionChanged"
            runat="server" >

            <SelectedDayStyle
                BackColor="CornSilk"
                ForeColor="Blue"
                Font-Name="Arial"
                Font-Bold="true"
                Font-Italic="true"/>

            <TodayDayStyle
                BackColor="CornSilk"
                ForeColor="Green"
                Font-Name="Arial"
                Font-Bold="true"
                Font-Italic="false"/>
        </asp:Calendar>

        <p/>
        <asp:Label id="lblCount" runat="server" />
        <p/>
        <asp:Label id="lbl1" runat="server" />
        <p/>
        <asp:Label id="lbl2" runat="server" />
        <p/>
        <table>
            <tr>
                <td>
                    Select a month:
                </td>
                <td>
                    <asp:DropDownList
                        id= "ddl"
                        AutoPostBack="true"
                        onSelectedIndexChanged = "ddl_SelectedIndexChanged"
                        runat="server">

                        <asp:ListItem text="January" value="1" />
                        <asp:ListItem text="February" value="2" />
                        <asp:ListItem text="March" value="3" />
                        <asp:ListItem text="April" value="4" />
                        <asp:ListItem text="May" value="5" />
                        <asp:ListItem text="June" value="6" />
                        <asp:ListItem text="July" value="7" />
                        <asp:ListItem text="August" value="8" />
                        <asp:ListItem text="September" value="9" />
                        <asp:ListItem text="October" value="10" />
                        <asp:ListItem text="November" value="11" />
                        <asp:ListItem text="December" value="12" />

                    </asp:DropDownList>
                </td>
                <td>
                    <asp:Button
                        id="btnTgif"
                        text="TGIF"
                        onClick="btnTgif_Click"
                        runat="server" />
                </td>
            </tr>
        </table>

    </form>
    </body>
</html>

Reply to this message...
 
    
Anthony Moore
You have run into a non-intuitive aspect of the Calendar OM. Your
work-around is the correct way to deal with it.

    void Page_Load(Object sender, EventArgs e)
    {
        cal.VisibleDate =3D cal.TodaysDate;
    }

The reason that this is required is that the default value of
VisibleDate is DateTime.Empty, and the behavior of the Calendar in this
instance is to display the current month. So, if you do any Date
arithmetic on VisibleDate before it has been assigned to, you will just
get back DateTime.Empty, which is a dummy date (1/1/0001).

The reason it was done this way was so that the calendar would display
the most logical month (ie. the current one) if you don't set it. Having
a default value of a property that changes goes against our component
model and does not work will with persistence in the WebForms designer.
So, we made DateTime.Empty the default and gave it this behavior.

Anthony Moore
.Net Framework

-----Original Message-----
From: Dan Hurwitz [mailto:Click here to reveal e-mail address]
Sent: Friday, May 04, 2001 6:15 AM
To: aspngbeta
Subject: [aspngbeta] Calendar Question

Hi All,

I am working with the Calendar control and have adapted some sample code
from the .Net Framework library. The adapted code is shown below.

If you run the aspx page, you will see there is a dropdownlist with the
months of the years, a label which shows the number of days selected,
and a
button to select all the Fridays in the month.

The problem is that if you click the button, the label updates properly
but
the selected days do not highlight. However, after using the dropdown
to
select a new month, the button does correctly highlight the selected
days.

If I uncomment the Page_Load method, then everything works correctly the
first time.

Why?

Thanks for any understanding you can shed.

--d

---------------------------------------
Calendar test.aspx
------------------

<%@ Page Language=3D"C#" %>
<html>
<script runat=3D"server">

    // Having this Page+Load makes the selected days visible first
time
    // the button is clicked. Otherwise selected days not visible
until
    // the second time the button is clicked.
//    void Page_Load(Object sender, EventArgs e)
//    {
//        cal.VisibleDate =3D cal.TodaysDate;
//    }

    void ddl_SelectedIndexChanged(Object sender, EventArgs e)
    {
        cal.SelectedDates.Clear();
        lblCountUpdate();
        cal.VisibleDate =3D new DateTime(cal.TodaysDate.Year,
Int32.Parse(ddl.SelectedItem.Value), 1);
    }

    void SelectionChanged(Object sender, EventArgs e)
    {
        lbl1.Text =3D "Today's Date is " +
cal.TodaysDate.ToShortDateString();

        if (cal.SelectedDate !=3D DateTime.MinValue)
            lbl2.Text =3D "The date selected is "+
cal.SelectedDate.ToShortDateString();

        lblCountUpdate();
    }

    void btnTgif_Click(Object sender, EventArgs e)
    {
        int currentDay =3D cal.VisibleDate.Day;
        int currentMonth =3D cal.VisibleDate.Month;
        int currentYear =3D cal.VisibleDate.Year;

        cal.SelectedDates.Clear();

        for (int i =3D 1; i <=3D
System.DateTime.DaysInMonth(currentYear,
currentMonth); i++ )
        {
            DateTime date =3D new DateTime(currentYear,
currentMonth, i);
            if (date.DayOfWeek =3D=3D DayOfWeek.Friday)
                cal.SelectedDates.Add(date);
        }

        lblCountUpdate();
    }

    void lblCountUpdate()
    {
        lblCount.Text =3D "Count of Days Selected: " +
cal.SelectedDates.Count.ToString();
    }

</script>

    <body>
    <form runat=3D"server">

        <asp:Calendar
            id=3D"cal"
            SelectionMode=3D"DayWeekMonth"
            ShowGridLines=3D"true"
            onSelectionChanged=3D"SelectionChanged"
            runat=3D"server" >

            <SelectedDayStyle
                BackColor=3D"CornSilk"
                ForeColor=3D"Blue"
                Font-Name=3D"Arial"
                Font-Bold=3D"true"
                Font-Italic=3D"true"/>

            <TodayDayStyle
                BackColor=3D"CornSilk"
                ForeColor=3D"Green"
                Font-Name=3D"Arial"
                Font-Bold=3D"true"
                Font-Italic=3D"false"/>
        </asp:Calendar>

        <p/>
        <asp:Label id=3D"lblCount" runat=3D"server" />
        <p/>
        <asp:Label id=3D"lbl1" runat=3D"server" />
        <p/>
        <asp:Label id=3D"lbl2" runat=3D"server" />
        <p/>
        <table>
            <tr>
                <td>
                    Select a month:
                </td>
                <td>
                    <asp:DropDownList
                        id=3D "ddl"
                        AutoPostBack=3D"true"
                        onSelectedIndexChanged =3D
"ddl_SelectedIndexChanged"
                        runat=3D"server">

                        <asp:ListItem
text=3D"January" value=3D"1" />
                        <asp:ListItem
text=3D"February" value=3D"2" />
                        <asp:ListItem
text=3D"March" value=3D"3" />
                        <asp:ListItem
text=3D"April" value=3D"4" />
                        <asp:ListItem text=3D"May"
value=3D"5" />
                        <asp:ListItem
text=3D"June" value=3D"6" />
                        <asp:ListItem
text=3D"July" value=3D"7" />
                        <asp:ListItem
text=3D"August" value=3D"8" />
                        <asp:ListItem
text=3D"September" value=3D"9" />
                        <asp:ListItem
text=3D"October" value=3D"10" />
                        <asp:ListItem
text=3D"November" value=3D"11" />
                        <asp:ListItem
text=3D"December" value=3D"12" />

                    </asp:DropDownList>
                </td>
                <td>
                    <asp:Button
                        id=3D"btnTgif"
                        text=3D"TGIF"
                        onClick=3D"btnTgif_Click"
                        runat=3D"server" />
                </td>
            </tr>
        </table>

    </form>
    </body>
</html>

| [aspngbeta] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspngbeta.asp =3D JOIN/QUIT

Reply to this message...
 
 
System.DateTime
System.DayOfWeek
System.EventArgs
System.Int32
System.Web.UI.WebControls.DropDownList
System.Web.UI.WebControls.ListItem
System.Windows.Forms.SelectionMode




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification