SIGN UP MEMBER LOGIN:    
ARTICLE

Web Server Calendar Control in ASP.NET 2.0

Posted by Sonu Chauhan Articles | Web Forms C# February 16, 2006
Most of the time we come across a situation when we need to extend the existing functionality of any control. So, in the following article we will see that how to extend the calendar control.
Reader Level:
Download Files:
 

The Calendar control is based on the .NET Framework DateTime object, and you can display any date between the years 0 and 9999 A.D.In the following article you will find that you can utilize the existing control as per you requirement.

In Visual Studio 2005 changing the template is quit easy as compare to MS VS 2003.You can see the templates below:

0.gif
 


1.gif

2.gif

3.gif

4.gif

5.gif

7.gif

After selecting the template for calender, editor will generate the following code for you:

<asp:Calendar ID="calSource" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66" BorderWidth="1px"  DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px" OnSelectionChanged="calSource_SelectionChanged">
            <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
            <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
            <SelectorStyle BackColor="#FFCC66" />
            <OtherMonthDayStyle ForeColor="#CC9966" />
            <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
            <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
            <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
</asp:Calendar>

In the same way add other controls also

 

<asp:DropDownList ID="drpMonthCal" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpCalMonth_SelectedIndexChanged" Width="101px">

        </asp:DropDownList>&nbsp;

        <asp:DropDownList ID="drpYearCal" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpCalYear_SelectedIndexChanged" Width="104px">

        </asp:DropDownList>

<asp:Label ID="lblDate" runat="server" Text="Label" Width="390px"></asp:Label>

 

Now you are ready with the web page.So you can start with the implementation.

 

protected void Page_Load(object sender, EventArgs e)

    {

         //Hide the title of the calendar control

        calSource.ShowTitle = false;   

       //Populate month and year dropdown list boxes which

       //replace the original calendar title

        if (!Page.IsPostBack)

        {   

            Populate_MonthddList();   

            Populate_YearddList();

        } 

        lblDate.Text = "Current date: " + calSource.TodaysDate;

    } 

    private void Populate_MonthddList()

    {

        drpMonthCal.Items.Add("January");

        drpMonthCal.Items.Add("February");

        drpMonthCal.Items.Add("March");

        drpMonthCal.Items.Add("April");

        drpMonthCal.Items.Add("May");

        drpMonthCal.Items.Add("June");

        drpMonthCal.Items.Add("July");

        drpMonthCal.Items.Add("August");

        drpMonthCal.Items.Add("September");

        drpMonthCal.Items.Add("October");

        drpMonthCal.Items.Add("November");

        drpMonthCal.Items.Add("December"); 

       // drpCalMonth.Items.FindByValue((objdate.Month.ToString())).Selected = true;

       // drpCalYear.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;

    } 

    private void Populate_YearddList()

    {

        //Year list can be extended

        int intYear ;          

        for(intYear =Convert.ToInt16(DateTime.Now.Year) - 20;intYear<=DateTime.Now.Year + 20;intYear++)

        {  

             drpYearCal.Items.Add(Convert.ToString(intYear));

        }

        drpYearCal.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true; 

    }

    protected void calSource_SelectionChanged(object sender, EventArgs e)

    {

        if (calSource.SelectedDates.Count == 1)

            // If one date is selected, display it.

            lblDate.Text = "Selected date: " + calSource.SelectedDate;

        else

            // If multiple dates are selected, display them.

            lblDate.Text = "Selected dates: " + calSource.SelectedDates[0] + " to " + calSource.SelectedDates[calSource.SelectedDates.Count - 1]; 

    }

    protected void drpCalMonth_SelectedIndexChanged(object sender, EventArgs e)

    {

        calSource.TodaysDate =Convert.ToDateTime (drpMonthCal.SelectedItem.Value + " 1, " + drpYearCal.SelectedItem.Value);

    }

    protected void drpCalYear_SelectedIndexChanged(object sender, EventArgs e)

    {

        calSource.TodaysDate = Convert.ToDateTime(drpMonthCal.SelectedItem.Value + " 1, " + drpYearCal.SelectedItem.Value);

    }

 

After writing the code Run the application.

Now you will see the following output screen :-

 

8.gif

 

9.gif

 

10.gif

Login to add your contents and source code to this article
share this article :
post comment
 

how to display event in calender  Column

Posted by vahini marriappan Sep 17, 2010

Thanks for your help my chum. i solved my problem after 3 hours net browsing, But no need to add dropdowns for change month/year. instead we can hide Title and we can add our own html row with controls (buttons) as same as calendar control header

Posted by Dumidu Perera Feb 11, 2009

Thanks for your help my chum. i solved my problem after 3 hours net browsing, But no need to add dropdowns for change month. instead we can hide Title and we can add ower own html row with controls (buttons) as same as calendar control header

Posted by Dumidu Perera Feb 11, 2009

Clicking on the calendar.zip file does not provide the source, but only the solution files. Can you please re-up the zip file? Thank you!

Posted by Raghavendran k Feb 10, 2009

Clicking on the calendar.zip file does not provide the source, but only the solution files. Can you please re-up the zip file? Thank you!

Posted by jmarquisette Jan 08, 2009
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor