Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Saturday 7 March 2015

Introduction: In this article I will explain How to show only current month's dates in calendar control or we can say how to remove or hide other previous, next  month's date in calendar control in asp.net using both C# and VB languages. 



Following task we will do in this article:
  • Show only current month’s dates and hide next previous month's date in calendar.
  • How to highlight current date i.e. today's date in calendar.
  • Make calendar control look attractive by setting its attributes.
By default asp.net calendar control display some dates of previous and next month as shown in following figure.

In this article I have explain how to hide other months dates.

Source Code:
In the design page, place a calendar control and set its attributes as shown below:

 <asp:Calendar ID="Calendar1"
         runat="server"
         Font-Names="verdana"           
         Font-Size="10pt"
         BorderColor="#CCCCCC"
         ShowGridLines="true"
         NextPrevStyle-Font-Size="14pt"
         NextPrevStyle-ForeColor="#FFFFFF"
         DayHeaderStyle-BackColor="#E5E5E5"
         DayHeaderStyle-ForeColor="#000000"
         TitleStyle-Height="30px"         
         TitleStyle-BackColor="#525252"
         TitleStyle-Font-Size="18px"
         TitleStyle-ForeColor="#FFFFFF"
         TodayDayStyle-BackColor="Red"
         TodayDayStyle-ForeColor="White"
         TodayDayStyle-Font-Bold="true"
         TodayDayStyle-Font-Size="14px" ondayrender="Calendar1_DayRender">
     </asp:Calendar>


Asp.Net C# Code to remove or hide other month date in calendar control

 In the code behind file write following code:

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {           
           Calendar1.TodaysDate = System.DateTime.Now;
        }
    }

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.IsOtherMonth)
        { 
            e.Cell.Controls.Clear();
            e.Cell.Text = string.Empty;
        }
    }

Asp.Net VB Section 

In the design page,  place a calendar control and set its attributes as shown below: 

<asp:Calendar ID="Calendar1"
         runat="server"
         Font-Names="verdana"           
         Font-Size="10pt"
         BorderColor="#CCCCCC"
         ShowGridLines="true"
         NextPrevStyle-Font-Size="14pt"
         NextPrevStyle-ForeColor="#FFFFFF"
         DayHeaderStyle-BackColor="#E5E5E5"
         DayHeaderStyle-ForeColor="#000000"
         TitleStyle-Height="30px"         
         TitleStyle-BackColor="#525252"
         TitleStyle-Font-Size="18px"
         TitleStyle-ForeColor="#FFFFFF"
         TodayDayStyle-BackColor="Red"
         TodayDayStyle-ForeColor="White"
         TodayDayStyle-Font-Bold="true"
         TodayDayStyle-Font-Size="14px" >
     </asp:Calendar>

Asp.Net VB Code to remove or hide other month's date in calendar control 
In the code behind file  write following code :

   Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then        
            Calendar1.SelectedDate = System.DateTime.Now
        End If
    End Sub


Protected Sub Calendar1_DayRender(sender As Object, e AsSystem.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
        If (e.Day.IsOtherMonth) Then
            e.Cell.Controls.Clear()
            e.Cell.Text = String.Empty
        End If

    End Sub

0 comments:

Post a Comment