posted by 써니루루 2007. 3. 10. 21:47

출처 : 뇌를 자극하는 ASP.NET


ASP.NET에서 제공하는 달력을 쉽게 만들 수 있는 컨트롤 입니다.



<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title> Calendar </title>
   
    <script runat="server">
       
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 바뀌면 날짜선택을 초기화한다.
            Calendar1.SelectionMode = (CalendarSelectionMode)DropDownList1.SelectedIndex;
           
            if(Calendar1.SelectionMode == CalendarSelectionMode.None)
                Calendar1.SelectedDates.Clear();
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            switch (Calendar1.SelectedDates.Count)
            {
                case 0: // None
                    Response.Write("어떤 일자도 선택되지 않았습니다.");
                    break;
                case 1: // Day
                    Response.Write("선택한 일자"
                        + Calendar1.SelectedDate.ToShortDateString()
                    );
                    break;
                case 7: // Week
                    Response.Write("선택한 주의 시작 일자"
                        + Calendar1.SelectedDate.ToShortDateString()
                    );
                    break;
                default: // Month
                    Response.Write("선택한 달의 시작 일자"
                        + Calendar1.SelectedDate.ToShortDateString()
                    );
                    break;
            }
        }

        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            CalendarDay d = e.Day;
            TableCell c = e.Cell;

            if (d.IsOtherMonth)
            {
                c.Controls.Clear();
            }
            else
            {
                string message = GetSchedule(d.Date.Month, d.Date.Day);
                c.Controls.Add(new LiteralControl("<br />" + message));
            }
        }

        string GetSchedule(int month, int day)
        {
            string schedule = "";

            if (month == 3 && day == 14)
            {
                schedule = "<a href='http://www.naver.com'>화이트데이</a>";
            }
            else if (month == 3 && day == 1)
            {
                schedule = "3.1절";
            }

            return schedule;
        }
</script>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <h3>Calendar의 SelectionChange의 이벤트를 사용하는 예제</h3>
       
        <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Value="None">None</asp:ListItem>
            <asp:ListItem Value="Day" Selected="True">Day</asp:ListItem>
            <asp:ListItem Value="DayWeek">DayWeek</asp:ListItem>
            <asp:ListItem Value="DayWeekMonth">DayWeekMonth</asp:ListItem>
        </asp:DropDownList>
       
        <br />
       
        <asp:Calendar ID="Calendar1" runat="server" Width="70%" OnSelectionChanged="Calendar1_SelectionChanged" OnDayRender="Calendar1_DayRender" TitleStyle-Font-Size="12px" TitleStyle-Font-Bold="true" DayStyle-VerticalAlign="top" DayStyle-Height="50px" SelectedDayStyle-BackColor="navy"></asp:Calendar>
        &nbsp;</div>
    </form>
</body>
</html>