Date Calendar

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 
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 Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

Date Calendar in ASP.NET TextBox with image icon Date Calendar in ASP.NET TextBox with image icon Reviewed by Anil Singh on 2:00 AM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^