Calendar control Selecting dates from database Disabling old dates selection Selecting multiple dates
Submitted by adri80 on Thursday, February 17, 2011 - 16:02.
A Calendar control example for Selecting dates from database, Disabling old dates selection, and Selecting multiple dates. This application is meant for beginners.
This example is done in a very easy to understand way to help beginners customize the Calendar control.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
///////////// Selecting multiple dates //////////////////
public static List UserSelectedDays = new List();
////////////_____________________________///////////////
protected void Page_Load(object sender, EventArgs e)
{
////////////////////////////// Selecting dates from database //////////////////////////////
string SqlConStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
SqlConnection Sqlcon = new SqlConnection(SqlConStr);
SqlDataAdapter SqlDA = new SqlDataAdapter("Select * From HolidayDate", Sqlcon);
DataSet dsPubs = new DataSet("Pubs");
SqlDA.FillSchema(dsPubs, SchemaType.Source, "Holiday");
SqlDA.Fill(dsPubs, "Holiday");
DataTable tblAuthors;
tblAuthors = dsPubs.Tables["Holiday"];
foreach (DataRow drCurrent in tblAuthors.Rows)
{
int day, month, year;
day = Convert.ToInt32(drCurrent["day"]);
month = Convert.ToInt32(drCurrent["month"]);
year = Convert.ToInt32(drCurrent["year"]);
SelectedDatesCollection theDates = Calendar1.SelectedDates;
theDates.Add(new DateTime(year, month, day));
}
//////////////////////_____________________________________________////////////////////////////
}
protected void ClearSelectedDates_Click(object sender, EventArgs e)
{
Calendar1.SelectedDates.Clear();
UserSelectedDays.Clear();
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
//////////// Disabling old dates selection ///////////////
if (e.Day.Date = DateTime.Now.Date)
{
e.Cell.BackColor = System.Drawing.ColorTranslator.FromHtml("#a9a9a9");
e.Day.IsSelectable = false;
}
/////////_____________________________________________//////////
///////////// Selecting multiple dates //////////////////
if (e.Day.IsSelected == true)
{
UserSelectedDays.Add(e.Day.Date);
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
///////////// Selecting multiple dates //////////////////
foreach (DateTime SelectedDays in UserSelectedDays)
{
Calendar1.SelectedDates.Add(SelectedDays);
}
////////////_____________________________///////////////
}
}
Add new comment
- 10 views