Click here to Skip to main content
15,881,655 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

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.

Thanks

...Register.aspx
<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 (dateField.style.display == 'none')
                dateField.style.display = 'block';
            else
                dateField.style.display = 'none';
        }
</script>

<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">


            <tr>
                <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>


            </tr>

        </table>

    </asp:Panel>
        </fieldset>
        </div>



    <div id="dateField" style="display:none;">

        <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="SelectionChanged_Date"></asp:Calendar>
</div>


</asp:Content>


Register.aspx.cs
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
Updated 28-Mar-13 8:20am
v2
Comments
Prasad Khandekar 28-Mar-13 15:13pm    
Hello,

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";
else
dateField.Style["display"] = "none";

}
}

Regards,

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900