Wednesday, 19 September 2012

Beginning .Net : Get or Retrieve selected date from calendar control with C# Examples and VB.Net Examples

You can get the selected date from calendar control.
There is a "SelectedDate" property of calendar control. This property is get and set property so you can also set date of calendar control. Calender control provided in

Here is example for this.
In this example we take calendar control , button and label. First you select date in control. By Default today date is highlighted but not selected. When you select date at that time date background color will changed after that On click of button we retrieve selected date from calendar control and display in label.
We can also format the date and display in our specific date format. in this example we display date in "dd/MM/yyyy" format.

ASPX Code :
        <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" 
            BorderColor="#FFCC66" DayNameFormat="Shortest" Font-Names="Verdana" 
            Font-Size="8pt" ForeColor="#663399" Height="200px" Width="220px" 
            BorderWidth="1px" ShowGridLines="True">
            <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
            <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
            <OtherMonthDayStyle ForeColor="#CC9966" />
            <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
            <SelectorStyle BackColor="#FFCC66" />
            <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" 
                ForeColor="#FFFFCC" />
            <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />

         <br />

         <asp:Button runat="server" ID="btnGetSelectedDate" Text="Get Selected Date" 
            onclick="btnGetSelectedDate_Click" /> &nbsp;&nbsp;&nbsp;
         <b>Date : </b><asp:Label runat="server" ID="lblDate" ></asp:Label>

C# Examples :
    protected void btnGetSelectedDate_Click(object sender, EventArgs e)
        lblDate.Text = Calendar1.SelectedDate.Date.ToString("dd/MM/yyyy");
    } Examples :
    Protected Sub btnGetSelectedDate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGetSelectedDate.Click
        lblDate.Text = Calendar1.SelectedDate.Date.ToString("dd/MM/yyyy")
    End Sub

Output : 

For Beginning .Net articles. Click Here...

This type of .Net Tips is very useful in day to day programming life.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.

No comments:

Post a comment