Thursday, January 01, 2009

ASP.NET: Calendar Control DayRender

ASP.NET: Calendar Control DayRender Sample Code
Method 1

Method 2Code Sample:

.cs page
DataSet ds = new DataSet();
protected void Page_Load(object sender, System.EventArgs e)
{
OleDbConnection myConnection = new OleDbConnection(ConfigurationSettings.AppSettings["DBConnStr"]);
string sql = "select eventid,eventdt,title1 from calendar";
OleDbDataAdapter da = new OleDbDataAdapter(sql, myConnection);
da.Fill(ds, "events");
}
protected void eventscalendar_DayRender(Object Src, DayRenderEventArgs E)
{
int i = 0;
StringBuilder strEvents = new StringBuilder();
strEvents.Append("");
foreach (DataRow row in ds.Tables["events"].Rows)
{
DateTime eventdate = (DateTime)row["eventdt"];
if (eventdate.Equals(E.Day.Date))
{
i++;
strEvents.Append("
" + row["title1"]);
strEvents.Append("
");
}
}
if (i > 0)
{
if (E.Day.Date != DateTime.Now.Date)
{
E.Cell.BackColor = Color.AliceBlue;
}
// strEvents.Append("
Events: " + i + "");
}
E.Cell.Controls.Add(new LiteralControl(strEvents.ToString()));
}

protected void eventcalendar_SelectionChanged(object sender, EventArgs e)
{
DataSet ds1 = new DataSet();
DateTime dt = eventcalendar.SelectedDate.Date;
OleDbConnection myConnection = new OleDbConnection(ConfigurationSettings.AppSettings["DBConnStr"]);
string sql = "select starttime,endtime,title1,description from calendar where eventdt='" + dt + "'";

OleDbDataAdapter da = new OleDbDataAdapter(sql, myConnection);
da.Fill(ds1, "events");
GridView1.DataSource = ds1;
GridView1.DataBind();
}


.aspx page:
<asp:calendar id="eventcalendar" TitleStyle-BackColor="#d18c25" NextPrevStyle-ForeColor="#ffffff" TitleStyle-Height="30px" ShowGridLines="true" runat="server" backcolor="white"
width="400px" height="400px" font-size="12px"
nextprevformat="ShortMonth" daynameformat="FirstTwoLetters"
firstdayofweek="Sunday" OnDayRender="eventscalendar_DayRender" OnSelectionChanged="eventcalendar_SelectionChanged">
<DayStyle Font-Names="TimesRoman" />
<SelectedDayStyle Font-Names="TimesRoman" Height="20px" Width="20px" Font-Size="11px" ForeColor="#680000" BackColor="#fff0ad" />
<titlestyle Font-Names="TimesRoman" ForeColor="#680000" font-size="20px" font-bold="true" borderwidth="2px" />
<dayheaderstyle Font-Names="TimesRoman" font-size="12px" font-bold="true" />
<todaydaystyle Font-Names="TimesRoman" backcolor="#fff282" forecolor="#680000" />
<WeekendDayStyle Font-Names="TimesRoman" BackColor="#fafad2" ForeColor="#ff0000" />

<othermonthdaystyle Font-Names="TimesRoman" forecolor="#cccccc" />
</asp:calendar>


No comments: