ARTICLE

Calendar control with Changed event in ASP.NET

Posted by Sapna Articles | ASP.NET using VB.NET December 28, 2010
In this article you will learn how to create a calendar and raise calendar selection changed event in ASP.NET.
 
Reader Level:

Calendar selection changed event

The Calendar selection changed event is raised when the user selects a day, a week, or an entire month by clicking the date selector controls. You can also add specify special occasions to your calendar like your birthday, anniversary, meetings etc and highlight them to remember. In this article you will learn how to create a calendar and raise calendar selection changed event in ASP.NET.   


Code Snippets


<%
@ Page language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="CalendarTest" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>Untitled Page</title>
</head><br />
<body>
 <form id="form1" runat="server">
  <div>
    <asp:Calendar ID="MyCalendar"
        runat="server"
        BackColor="White"
        BorderColor="White"
        BorderWidth="1px"
        Font-Names="Verdana"                                
        Font-Size="9pt"
        ForeColor="Black"
        Height="190px"
        NextPrevFormat="FullMonth"
        OnDayRender="MyCalendar_DayRender"
        OnSelectionChanged="MyCalendar_SelectionChanged"
        SelectionMode="DayWeek"
        Width="350px">
     <SelectedDayStyle BackColor="#333399" ForeColor="White" />
     <OtherMonthDayStyle ForeColor="#999999" />
     <TodayDayStyle BackColor="#CCCCCC" />
     <NextPrevStyle Font-Bold="True"
        ForeColor="#333333"
        VerticalAlign="Bottom" />
     <DayHeaderStyle Font-Bold="True" Font-Size="8pt" />
     <TitleStyle BackColor="White"
        BorderColor="Black"
        BorderWidth="4px"
        Font-Bold="True"
        Font-Size="12pt"
        ForeColor="#333399" />
     </asp:Calendar>
     <br />
     <asp:Label ID="lblDates" runat="server"></asp:Label>
   </div>
 </form>
</body>
</html>

VB Code

Imports
System
Imports System.Data
Imports System.Configuratio
Imports System.We
Imports System.Web.Security
Imports
System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Partial Public Class CalendarTest
  Inherits System.Web.UI.Page
  Protected Sub MyCalendar_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
    lblDates.Text = "You have selected:
    For Each dt As Date In MyCalendar.SelectedDates
       lblDates.Text += dt.ToLongDateString() & "<br />"
    Next dt
  End Sub
  Protected Sub MyCalendar_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs)
  If e.Day.Date.Day = 11 AndAlso e.Day.Date.Month = 2 Then
    e.Cell.BackColor = System.Drawing.Color.LightBlue
    Dim lbl As New Label()
    lbl.Text = "<br />My Birthday!"
    e.Cell.Controls.Add(lbl)
  End If
  End Sub
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  End Sub
End Class

ASP.NET Calender Control with changed event

calender.gif

I hope you will like this.

Login to add your contents and source code to this article
share this article :
post comment
 
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.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor