Click here to Skip to main content
11,487,793 members (76,979 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 28-Mar-13 8:39am
Timtope335
Edited 28-Mar-13 9:20am
(no name)34.1K
v2
Comments
Prasad Khandekar at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,193
1 OriginalGriff 8,287
2 Sascha Lefèvre 3,334
3 Maciej Los 2,946
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web03 | 2.8.150520.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