Calendar in ASP.NET

Date Calendar in ASP.NET TextBox with image icon

Hello everyone, 

In this post, i'm going to share the code-sample for
 date calendar date-time calendertextbox in asp.net with c#
 
This example is simple and easily understandable code using asp.net with c# 

Table of Contents 


1. In the 1st step, code-sample for C# (.cs page)
2. In the 2nd step, code-sample for aspx page

In the 1st step, we write the code for the cs file in asp.net page i.e. .aspx.cs


namespace myWebApplication
{
    public partial class _Default : BaseFeature
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CalOne.Visible = false;
            CalTwo.Visible = false;
        }

        protected void imgButtonOne_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (txtDate.Text.Trim() != "")
                    CalOne.SelectedDate = Convert.ToDateTime(txtDate.Text);
            }
            catch (Exception e)
            {
                throw e;
            }
            CalOne.Visible = true;
        }

        protected void CalOne_SelectionChanged(object sender, EventArgs e)
        {
            txtDate.Text = CalOne.SelectedDate.ToShortDateString();
            CalOne.Visible = false;
        }

        protected void imgButtonTwo_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (txtFronDate.Text.Trim() != "")
                    CalTwo.SelectedDate = Convert.ToDateTime(txtFronDate.Text);
            }
            catch(Exception e)
            {
                throw e;
            }
            CalTwo.Visible = true;
        }

        protected void CalTwo_SelectionChanged(object sender, EventArgs e)
        {
            txtFronDate.Text = CalTwo.SelectedDate.ToShortDateString();
            CalTwo.Visible = false;
        }

    }

}


In the 2nd step, we write the code for the view in asp.net page i.e. .aspx page

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="MainContent">

    <asp:TextBox ID="txtDate" runat="server" Width="70"></asp:TextBox>
    <asp:ImageButton ID="imgButtonOne" runat="server" CausesValidation="false"Height="19px" Width="15px" ImageUrl="images/cal.gif" OnClick="imgButtonOne_Click" />

    <asp:TextBox ID="txtDateFrom" runat="server" Width="70"></asp:TextBox>
    <asp:ImageButton ID="imgButtonTwo" runat="server" ImageUrl="images/cal.gif"OnClick="imgButtonTwo_Click" />

    <asp:Calendar ID="CalOne" runat="server"OnSelectionChanged="CalOne_SelectionChanged">
    </asp:Calendar>

    <asp:Calendar ID="CalTwo" runat="server"OnSelectionChanged="CalTwo_SelectionChanged">
    </asp:Calendar>

</asp:Content>


Notes

download and add to this image in "images" folder in our  application for display to calender textbox 


http://3.bp.blogspot.com/-J1uWZ16IrY8/UbxeJUWkdXI/AAAAAAAAAMs/LXK3JwYtM2s/s1600/Date+Calendar.gif



ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.