- //Default.aspx
- <form id="form1" runat="server">
- <div>
- <asp:Calendar ID="Calendar" runat="server" OnDayRender="DayRender" ShowGridLines="true" />
- </div>
- </form>
- //Default.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class _Default : System.Web.UI.Page
- {
- DataTable _DayEventsTable;
- protected void Page_Load(object sender, EventArgs e)
- {
- _DayEventsTable = new DataTable();
- _DayEventsTable.Columns.Add("Date");
- _DayEventsTable.Columns.Add("Title");
- _DayEventsTable.Rows.Add(DateTime.Now.AddDays(-2).Date.ToString(), "Meeting with Boss");
- _DayEventsTable.Rows.Add(DateTime.Now.Date.ToString(), "Lunch with Suzan");
- _DayEventsTable.Rows.Add(DateTime.Now.AddDays(2).Date.ToString(), "Trip to Paris!");
- }
- protected void DayRender(object sender, DayRenderEventArgs e)
- {
- foreach (DataRow Row in _DayEventsTable.Rows)
- {
- string Date = Row["Date"].ToString();
- string Title = Row["Title"].ToString();
- if (Date == e.Day.Date.ToString())
- {
- e.Cell.Controls.Add(new LiteralControl("<p>" + Title + "</p>"));
- }
- }
- }
- }
Add Text To Day Cell In Calendar Control In C#
This code snippet shows how to add text to the day cell in a Calendar control. It uses the OnDayRender event to add text to a day cell from a DataTable object.
No comments:
Post a Comment