Search code examples
asp.netcalendarextender

How to set "VisibleDate" property of asp:Calendar control?


I am developing a ASP.NET web site, in which I have used a AJAX control Tool kit's CalendarExtender to select a date in asp:TextBox. I want to set the VisibleDate property of the asp:Calendar control based on the selected date from the CalendarExtender control. I request you to help me to achieve this functionality. Or Is there any way to post back the page on selection of date from CalendarExtender control so that I can handle TextChanged event in the codebehind and set the VisibleDate property at within this event handler? Thanks


Solution

  • I would use an asynchronous postback on TextChanged-event to set the VisibleDate property:

    aspx:

    <asp:UpdatePanel ID="UdpDatePanel" runat="server" UpdateMode="conditional" ChildrenAsTriggers="false"  >
       <ContentTemplate>
          <asp:Calendar ID="Calendar1"  runat="server" />
          <asp:TextBox ID="TxtDate" AutoPostBack="true" runat="server" />
          <asp:CalendarExtender ID="CalendarExtender1" TargetControlID="TxtDate" runat="server" />
       </ContentTemplate> 
       <Triggers>
            <asp:AsyncPostBackTrigger ControlID="TxtDate" EventName="TextChanged" />
       </Triggers>
    </asp:UpdatePanel> 
    

    codebehind:

    Public Partial Class CalendarDemo
        Inherits System.Web.UI.Page
    
        Private Sub TxtDate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtDate.TextChanged
            Dim d As Date
            If Date.TryParse(Me.TxtDate.Text, d) Then
                Me.Calendar1.VisibleDate = d
            End If
        End Sub
    
    End Class
    

    On this way it keeps performant and you don't have to mess around with javascript that might change in future releases of asp.net-ajax toolkit.