0
votes

i'm trying to come up with a DateTime Picker in ASP.Net using textBox, Calendar, Button and Updatefields.

if a user clicks a button the panel with a hidden calendar will become visible, and if a user selects a date calendar's visibility will become false and he date will be assigned to textBox.

My problem comes if a user wants to change month because in this case calendar will disappear because selectionChange is being triggered.

My question is how can I fish out the changing of the month and bound the hiding of the panel only if a date is selected?

thank you very much in advance.

here are the code samples:

front part:

<asp:UpdatePanel ID="UpdatePanel2" runat="server">
        <ContentTemplate>
    <asp:TextBox ID="TextBox2" runat="server" />
            <asp:ImageButton ID="Imbut" runat="server" ImageUrl="imagesNew/calender.png" Height="17px" Width="17px" />
    </ContentTemplate>
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Calendar1" EventName="SelectionChanged" />
        </Triggers>
    </asp:UpdatePanel>

    <br />


    <br />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <asp:Panel ID="Panel1" runat="server">
            <asp:Calendar ID="Calendar1" runat="server" Height="129px" Width="209px">
            </asp:Calendar>
        </asp:Panel>
        </ContentTemplate>
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Imbut" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>

back part

Protected Sub Imbut_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Imbut.Click
    Panel1.Visible = True
End Sub

Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Calendar1.SelectionChanged

        TextBox2.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy")
        Panel1.Visible = False

End Sub
1

1 Answers

0
votes

I struggled with a similar problem. The problem is (as you stated) on the postback the control gets reset. I never found a means to do this elegantly with asp.net controls. I was able to accomplish this using the jQuery calendar and asp:hidden field. Keeping it client side via javascript I was able to allow the user to navigate the calendar change dates etc. When a date was selected the values were set to both a textbox within the ui as well as a hidden field then on submission, the value within the textbox or hidden field was picked up server side and used (in my situation) as a filter parameter.