如何:在 Calendar 控件中显示数据库中的选定日期
1个回答
展开全部
当您选定一个月份时,将获取当前所选月份的日期,此日期范围是基于 Calendar 控件的 VisibleDate 属性定义的,该属性返回当前月的第一个日期。 每次用户导航到一个新的月份时,代码就会读取该月份的假日。 在 DayRender 事件中,代码会将当前呈现的日期与从数据库返回的日期进行比较。 如果有匹配的日期,则用特殊颜色标记这一天。 注意 需要对 Calendar 控件标记添加 OnDayRender 属性,以便使用 DayRender 事件。 例如,代码应类似于下面的示例: <asp:Calendar id="Calendar1" OnDayRender=" Calendar1_DayRender" runat="server" ></asp:Calendar>Protected dsHolidays As DataSet Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then Calendar1.VisibleDate = DateTime.Today FillHolidayDataset() End If End Sub Protected Sub FillHolidayDataset() Dim firstDate As New DateTime(Calendar1.VisibleDate.Year, _ Calendar1.VisibleDate.Month, 1) Dim lastDate As DateTime = GetFirstDayOfNextMonth() dsHolidays = GetCurrentMonthData(firstDate, lastDate) End Sub Protected Function GetFirstDayOfNextMonth() As DateTime Dim monthNumber, yearNumber As Integer If Calendar1.VisibleDate.Month = 12 Then monthNumber = 1 yearNumber = Calendar1.VisibleDate.Year + 1 Else monthNumber = Calendar1.VisibleDate.Month + 1 yearNumber = Calendar1.VisibleDate.Year End If Dim lastDate As New DateTime(yearNumber, monthNumber, 1) Return lastDate End Function Protected Sub Calendar1_VisibleMonthChanged(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) _ Handles Calendar1.VisibleMonthChanged FillHolidayDataset() End Sub Function GetCurrentMonthData(ByVal firstDate As DateTime, _ ByVal lastDate As DateTime) As DataSet Dim dsMonth As New DataSet Dim cs As ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings("ConnectionString1") Dim connString As String = cs.ConnectionString Dim dbConnection As New SqlConnection(connString) Dim query As String query = "SELECT HolidayDate FROM Holidays " & _ " WHERE HolidayDate >= @firstDate AND HolidayDate < @lastDate" Dim dbCommand As New SqlCommand(query, dbConnection) dbCommand.Parameters.Add(New SqlParameter("@firstDate", firstDate)) dbCommand.Parameters.Add(New SqlParameter("@lastDate", lastDate)) Dim sqlDataAdapter As New SqlDataAdapter(dbCommand) Try sqlDataAdapter.Fill(dsMonth) Catch End Try Return dsMonth End Function Protected Sub Calendar1_DayRender(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) _ Handles Calendar1.DayRender Dim nextDate As DateTime If Not dsHolidays Is Nothing Then For Each dr As DataRow In dsHolidays.Tables(0).Rows nextDate = CType(dr("HolidayDate"), DateTime) If nextDate = e.Day.Date Then e.Cell.BackColor = System.Drawing.Color.Pink End If Next End If End Subprotected DataSet dsHolidays; protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { Calendar1.VisibleDate = DateTime.Today; FillHolidayDataset(); } } protected void FillHolidayDataset() { DateTime firstDate = new DateTime(Calendar1.VisibleDate.Year, Calendar1.VisibleDate.Month, 1); DateTime lastDate = GetFirstDayOfNextMonth(); dsHolidays = GetCurrentMonthData(firstDate, lastDate); } protected DateTime GetFirstDayOfNextMonth() { int monthNumber, yearNumber; if(Calendar1.VisibleDate.Month == 12) { monthNumber = 1; yearNumber = Calendar1.VisibleDate.Year + 1; } else { monthNumber = Calendar1.VisibleDate.Month + 1; yearNumber = Calendar1.VisibleDate.Year; } DateTime lastDate = new DateTime(yearNumber, monthNumber, 1); return lastDate; } protected DataSet GetCurrentMonthData(DateTime firstDate, DateTime lastDate) { DataSet dsMonth = new DataSet(); ConnectionStringSettings cs; cs = ConfigurationManager.ConnectionStrings["ConnectionString1"]; String connString = cs.ConnectionString; SqlConnection dbConnection = new SqlConnection(connString); String query; query = "SELECT HolidayDate FROM Holidays " + _ " WHERE HolidayDate >= @firstDate AND HolidayDate < @lastDate"; SqlCommand dbCommand = new SqlCommand(query, dbConnection); dbCommand.Parameters.Add(new SqlParameter("@firstDate", firstDate)); dbCommand.Parameters.Add(new SqlParameter("@lastDate", lastDate)); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(dbCommand); try { sqlDataAdapter.Fill(dsMonth); } catch {} return dsMonth; } protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) { DateTime nextDate; if(dsHolidays != null) { foreach(DataRow dr in dsHolidays.Tables[0].Rows) { nextDate = (DateTime) dr["HolidayDate"]; if(nextDate == e.Day.Date) { e.Cell.BackColor = System.Drawing.Color.Pink; } } } } protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e) { FillHolidayDataset(); } 此示例基于当前显示的月份的日期生成一个查询。 VisibleDate 属性返回当前月的第一个日期。 (在用户导航到日历中之前,不会设置 VisibleDate 属性,因此在第一次显示该页时,代码会手动设置 VisibleDate 属性。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询