Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an ASP calendar on my webpage along with a textbox and a small image button, I want to click the image button and select the date from the calendar to display in the textbox.So far its working fine and I can select the date and display in the textbox. But, my problem is that if I have already selected a date and it is displayed in the textbox,if I click on the same date again,the calendar wont close by itself.I have to select some other date to close it. How do i make it work so that it will close if I click on the selected date again and I also want to close the calendar if I click anywhere else on the webpage as well. I am not using the Ajax calendar control since Ajax is not available on our server. Please help me with a solution.

Here is my source code:

XML
<asp:Panel ID="pnlinsert" runat="server">
   <table>
   <tr>
   <td>
       <asp:Label ID="lblstart" runat="server" Text="Start Date"></asp:Label></td>
       <td>
           <asp:TextBox ID="txtstartdate" runat="server"></asp:TextBox></td>
           <td>
         <asp:ImageButton ID="imgbtnstart" runat="server"
                   ImageUrl="~/images/calender.png" AlternateText="Click here to show calendar" OnClick="imgbtnstart_Click" Height="25px"
                   Width="16px"/>
           <td>

               <asp:Calendar ID="cdrcalendar" runat="server" Visible="false" OnSelectionChanged="cdrcalendar_SelectionChanged"></asp:Calendar>

           </td>


   </tr>
   </table>
   </asp:Panel>



and this is my code for imgbtnstart_Click and cdrcalendar_SelectionChannged.

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtstartdate.Text = DateTime.Today.ToString("MM-dd-yyyy");
        }
    }
    protected void cdrcalendar_SelectionChanged(object sender, EventArgs e)
    {
      txtstartdate.Text = cdrcalendar.SelectedDate.ToString("MM-dd-yyyy");
      cdrcalendar.Visible = false;


    }
    protected void imgbtnstart_Click(object sender, ImageClickEventArgs e)
    {
        cdrcalendar.Visible = true;
    }
Posted

You're in a bind there because when you click the same date, the SelectionChanged event doesn't fire (the value isn't changing). Though it doesn't immediately appear to be supported, you could do something like handle a click event on the calendar or its container, but then you don't really want to close the control when the user is just trying to navigate through the years or months. The good news is that clicking on the same date does a postback no matter what, so it's going through your page events, and you can catch it and handle it there. So while there may be a better way to do it, adding this code to your Page_Load accomplishes the goal:
VB
If Request.Params("__EVENTTARGET") IsNot Nothing AndAlso Request.Params("__EVENTTARGET").EndsWith(cdrcalendar.ID) Then
    If Session("CALENDAR_LASTCLICK") = Request.Params("__EVENTARGUMENT") Then
        cdrcalendar.Visible = False
    Else
        Session("CALENDAR_LASTCLICK") = Request.Params("__EVENTARGUMENT")
    End If
End If
 
Share this answer
 
v2
I solved this by adding the following line in the Page_Load event

cdrcalendar.SelectedDates.Clear();

Its working fine now.Thanks a lot!
 
Share this answer
 

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