Click here to Skip to main content
11,922,641 members (56,596 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET

I have the code below which is meant to populate a textbox with the date of birth from an asp calendar but the calendar keeps disappearing when i try to change the month. I would appreciate any suggestion to fix this as i am new to asp.


<asp:Content ID="Content1" runat="server" contentplaceholderid="ContentPlaceHolder1" EnableViewState="True">
    <script type="text/javascript">
        function popupCalendar() {
            var dateField = document.getElementById('dateField');
            // toggle the div
            if ( == 'none')
       = 'block';
       = 'none';
<h2 style="text-align: center">ADD STAFF</h2><br />
    <div class="container">
    <fieldset> <legend>Personal Details</legend>
    <asp:Panel ID="Panel1" runat="server">
        <table border="0" id="table2">

                <td class="td1">Date Of Birth </td>
                <td class="td2"><asp:TextBox ID="CalTxt" runat="server" AutoPostBack="True" MaxLength="20" /><img src="Images/date.ico" onclick="popupCalendar()" style="height: 30px" />  </td>


    <div id="dateField" style="display:none;">
        <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="SelectionChanged_Date"></asp:Calendar>


protected void Page_Load(object sender, EventArgs e)
        protected void SelectionChanged_Date(object sender, EventArgs e)
            string date = Calendar1.SelectedDate.ToString("dd/MM/yyyy");
            CalTxt.Text = date.ToString();
Posted 28-Mar-13 8:39am
Edited 28-Mar-13 9:20am
Prasad Khandekar 28-Mar-13 15:13pm

The calendar is disappearing because on change of month it's triggering a postback. You can try adding code similar to shown below in your page_load to keep the calendar visible.

protected void Page_Load(object sender, EventArgs e)
if (IsPostBack)
if (txtDate.Text == "")
dateField.Style["display"] = "block";
dateField.Style["display"] = "none";



This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151125.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100