Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Saturday 7 March 2015

Introduction: In this article I will explain how 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.

Source Code:

In the design page, 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>

Or (Output will be 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
 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;
        }
    }
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
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


 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

0 comments:

Post a Comment