Onlineatoz.net | ToolBox Standard Control Calendar

ToolBox Standard Control Calendar

Calendar Web Server Control

In ASP.NET page,The Calendar Web server control can be used to display a traditional one-month calendar.its a one of the major control in ASP.NET web page. Users can view and select dates on the calendar.The Calendar control is based on the .NET Framework DateTime object.Effectively, you can display any date between the years 0 and 9999 A.D. a sample caledar can be look like below:

images/Article_Asp_Calendar.gif

Date Types

The Calendar control can be provide the dates for one month with six weeks appearing at once. The control supports several types of dates, which are described in the following table.

  • TodaysDate -By default, this is set to match the current date on the server. However, you can adjust it so that the date appears correctly for a different locale.
  • VisibleDate -This date determines which month appears in the calendar.
  • SelectedDate -This is the date or date range that the user chooses.

Set the Calendar's Appearance

You are able to customize the properties to change the colors, size, text, and other visual features of the calendar.the following ways helps you to do this,

  • Setting properties -You can set properties to display grid lines, change which day is displayed as the first day of the week, and change the appearance of the month and day names.
  • Setting extended style properties -You can use properties derived from the Style object to set the appearance of particular elements in the calendar, such as the current date or the title bar containing the month and navigation links.
  • Customizing the rendering of individual days -it raises an event that you can handle to modify the stream being rendered to the browser.
Select Dates Programmatically

to select a single date

Calendar1.SelectedDate = DateTime.Today;

to select set of date

DateTime aDate = DateTime.Today;
SelectedDatesCollection theDates = Calendar1.SelectedDates;
theDates.Clear();
for (int i = 0;i <= 6;i++)
{
      theDates.Add(aDate.AddDays(i));
}
                 

to clear date selection

Calendar1.SelectedDates.Clear();
 
Display Selected Dates from a Database

To display database data in the Calendar control

  • Use ADO.NET types to connect to a database and query for the dates to display.
  • In the Calendar control's DayRender event, compare the date currently being rendered against the data you have retrieved from the database. If there is a match, customize the day display.

the following code snippets represent this

protected DataSet DataSet1;

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        Calendar1.VisibleDate = DateTime.Today;
        SetDataset();
    }
}

protected void SetDataset()
{
    DateTime firstDate = new DateTime(Calendar1.VisibleDate.Year, 
        Calendar1.VisibleDate.Month, 1);
    DateTime lastDate = GetDateOfNextMonth();
    DataSet1 = GetDate(firstDate, lastDate);
}

protected DateTime GetDateOfNextMonth()
{
    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 GetDate(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(DataSet1 != 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)
{
    SetDataset();
}
                 
 


Posted by: Admin
Posted on: 9/11/2011 at 7:38 PM
Categories: Asp.net
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed