How to highlight current date in calendar control in asp.net C#, VB

highlight today date in asp.net calendarIntroduction: In this article i am going to share the code trick to highlight current/today's date in asp.net calendar using code behind or by setting the calendar's attributes in asp.net using both C# and VB languages.



Implementation: Let's create a simple web page to demonstrate the concept.
  • In the design page (default.aspx) place a calendar control and set its attributes as shown below: 

<asp:Calendar ID="Calendar1"
         runat="server"
         TodayDayStyle-BackColor="Yellow"
         TodayDayStyle-ForeColor="Red"
         TodayDayStyle-Font-Bold="true"
         TodayDayStyle-Font-Size="20px">
</asp:Calendar>

--> There is another way to set the same attributes as shown below. You can use any of the two ways. Output will be the same.

<asp:Calendar ID="Calendar1"
         runat="server">
         <TodayDayStyle BackColor="Yellow" ForeColor="Red" Font-Bold="true" Font-Size="20px" />
         </asp:Calendar>


Asp.Net C# Code to highlight current date in calendar control
  •  In the code behind file (default.aspx.cs)  write the code as:

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Calendar1.SelectedDate = System.DateTime.Now;
           // or you can also set the current date as:
           Calendar1.TodaysDate = System.DateTime.Now;
        }
    }

Note: You can also set the TodayDayStyle property from code as:

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

            Calendar1.TodayDayStyle.BackColor = System.Drawing.Color.Yellow
            Calendar1.TodayDayStyle.ForeColor = System.Drawing.Color.Red
            Calendar1.TodayDayStyle.Font.Bold = True
        }
    }


Asp.Net VB Code to highlight current date in calendar control
  •  In the code behind file(default.aspx.vb)  write the code as:

   Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then        
            Calendar1.SelectedDate = System.DateTime.Now
           // or you can also set the current date using the line below:                                                                     Calendar1.TodaysDate = System.DateTime.Now
        End If
    End Sub

Note: You can also set the TodayDayStyle property from code as:

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

            Calendar1.TodayDayStyle.BackColor = System.Drawing.Color.Yellow
            Calendar1.TodayDayStyle.ForeColor = System.Drawing.Color.Red
            Calendar1.TodayDayStyle.Font.Bold = True
        End If
    End Sub


 Now over to you:
" I hope you have got How to highlight current date in Asp.Net Calendar control and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

2 comments

Click here for comments
April 26, 2014 ×

Your welcome Ipsita pani...:)

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..