如何让新活动显示在日历上

时间:2011-02-01 04:58:38

标签: vb.net calendar

我正在尝试使用VB.net及其日历更加舒适。我编码到我有一个日历显示的几个事件,如果您选择一天或更改月份,日历上方的标签反映了更改。我想要做的是允许用户在计划中添加新事件。目前我不知道如何让新活动显示在日历上。

代码:

Public Class Calendar
    Inherits System.Web.UI.Page

    Dim schedule As New Hashtable
    Dim _scheduleData As Hashtable


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _scheduleData = GetSchedule()

        Calendar1.Caption = "Personal Schedule"
        Calendar1.FirstDayOfWeek = WebControls.FirstDayOfWeek.Sunday
        Calendar1.NextPrevFormat = NextPrevFormat.ShortMonth
        Calendar1.TitleFormat = TitleFormat.MonthYear
        Calendar1.ShowGridLines = True
        Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Left
        Calendar1.DayStyle.VerticalAlign = VerticalAlign.Top
        Calendar1.DayStyle.Height = New Unit(75)
        Calendar1.DayStyle.Width = New Unit(100)
        Calendar1.OtherMonthDayStyle.BackColor = Drawing.Color.DarkSlateBlue
        Calendar1.TodaysDate = New DateTime(2011, 2, 1)
        Calendar1.VisibleDate = Calendar1.TodaysDate

    End Sub

    Private Function GetSchedule() As Hashtable

        schedule("2/14/2011") = "Valentines Day"
        schedule("2/1/2011") = "Presentation"
        schedule("5/16/2011") = "Birthday"

        Return schedule

    End Function

    Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
        If Not _scheduleData(e.Day.Date.ToShortDateString()) Is Nothing Then

            Dim lit = New Literal()
            lit.Text = "<br />"
            e.Cell.Controls.Add(lit)

            Dim lbl = New Label()
            lbl.Text = _scheduleData(e.Day.Date.ToShortDateString())
            lbl.Font.Size = New FontUnit(FontSize.Small)
            e.Cell.Controls.Add(lbl)

        End If

    End Sub


    Private Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
        LabelAction.Text = "Selection changed to: " + Calendar1.SelectedDate.ToShortDateString

    End Sub


    Private Sub Calendar1_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles Calendar1.VisibleMonthChanged
        LabelAction.Text = "Month changed to: " + e.NewDate.ToShortDateString()

    End Sub

    Private Sub addButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles addButton.Click
        Dim day As Date
        Dim name As String

        day = dayText.Text
        name = nameText.Text

        schedule(day) = name

    End Sub



End Class

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Calendar.aspx.vb" Inherits="WebApplication2.Calendar" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Label ID="LabelAction" runat="server"></asp:Label>
        <br />
        <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        <asp:Label ID="Label1" runat="server" Text="Date"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label2" runat="server" Text="Name of Event"></asp:Label>
        <br />
        <asp:TextBox ID="dayText" runat="server" Width="66px"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="nameText" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="addButton" runat="server" Text="Add Event" />

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

1 个答案:

答案 0 :(得分:0)

我解决了它但使用字典

将调度程序定义为以下

Dim schedule As New Dictionary(Of Date, String)

对于GetSchedule,我将其更改为此

Private Sub GetSchedule()
    schedule(New Date(2011, 2, 14)) = "Valentines Day"
    schedule(New Date(2011, 1, 2)) = "Presentation"
    schedule(New Date(2011, 5, 16)) = "Birthday"
End Function

在月份添加事件以检查所有事件是否在可见月份内

    Private Sub Calendar1_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles Calendar1.VisibleMonthChanged
        GetSchedule()
        Dim StartDate As Date = Calendar1.VisibleDate
        Dim EndDate As New Date(Calendar1.VisibleDate.Year, Calendar1.VisibleDate.Month, Date.DaysInMonth(Calendar1.VisibleDate.Year, Calendar1.VisibleDate.Month))
        For Each Item As KeyValuePair(Of Date, String) In schedule
            If Item.Key >= StartDate And Item.Key <= EndDate Then

                MsgBox(Item.Value)
                'Add view actions of this month
            End If
        Next
    End Sub

请测试一下,看看它是如何与你合作的

注意:它适用于可见月份,并且不会显示其他月份