Introduction: 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.
In previous article i explained the use of CalendarExtendar control of Ajax in asp.net and Get age in years,months,days,hours and seconds from DOB and Convert Rupees,Currency or Numbers to Words and Bind state categories and cities sub categories in single dropdownlist and Create crystal reports in visual studio 2010 and Generate random number in asp.net
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."
2 comments
Click here for commentsTHANKS.
ReplyYour welcome Ipsita pani...:)
ReplyIf 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..