戰地連結︰ Home My Flickr NBA.com About

2009年1月21日星期三

Restrict date range in Calendar

Overview

By default, Calendar control won't allow you to restrict the selectable date range. However, we can achieve this by doing two things:

  1. Restrict user from navigating to a month outside the range
  2. Restrict user from selecting a day outside the range

Reference

Visual Studio .NET - restrict date range in Calendar - eggheadcafe

Disable calendar day select link - eggheadcafe

Steps

Restrict user from navigating to a month outside the range

To achieve this, we can set the "NextMonthText" and "PrevMonthText" fields of the Calendar control to hide the link navigating to next or previous month.

Code Snippet:

protected void Cal_PreRender(object sender, EventArgs e)

        {

 

            if (Cal.VisibleDate.Year > 2009 || (Cal.VisibleDate.Year == 2009 && Cal.VisibleDate.Month >= 12))

            {

                Cal.NextMonthText = string.Empty;

            }

            else

            {

                Cal.NextMonthText = ">";

            }

 

            if (Cal.VisibleDate.Year < 2000 || (Cal.VisibleDate.Year == 2000 && Cal.VisibleDate.Month <= 1))

            {

                Cal.PrevMonthText = string.Empty;

            }

            else

            {

                Cal.PrevMonthText = "<";

            }

        }

Restrict user from selecting a day outside the range

This can be achieved by controlling the "IsSelectable" field in the "onDayRender" event handler of the Calendar.

Code Snippet:

protected void Cal_DayRender(object sender, DayRenderEventArgs e)

        {

            if (e.Day.Date.Year == 2009)

            {

                e.Day.IsSelectable = false;

            }

        }

ListView Control vs Repeater Control

Overview

Repeater control is one of my favorite ASP.NET controls because of its flexibility in UI design. However it lack some useful functions like "Sorting", "Paging" & "Data modification".

Now in ASP.NET 3.5 we have a ListView control which seems to do all of the Repeater's jobs and more. Below are some article talking about ListView control.

Reference

The asp:ListView control (Part 1) - Scott Guthrie

The ListView Dominates The Repeater - SingingEels

ListView Web Server Control Overview - MSDN