locked
Calendar inside UpdatePanel not working as expected RRS feed

  • Question

  • User944339287 posted

    Hi guys, my calendar is not working as expected if i placed it inside the UpdatePanel. (SelectionChanged is not firing)

    It's working fine if without the UpdatePanel. Please advise.

    HTML

        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Calendar ID="Calendar" runat="server">                    
                    <SelectedDayStyle BackColor="#CC3333" ForeColor="White" />
                </asp:Calendar>        
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Calendar" EventName="SelectionChanged" />
            </Triggers>
            </asp:UpdatePanel>
        </div>
        </form>

    Code Behind

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Page.IsPostBack = False Then
                Me.Calendar.SelectedDate = DateTime.Now.Date
            End If
        End Sub
    
        Protected Sub Calendar_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar.SelectionChanged
            Response.Write(Me.Calendar.SelectedDate)
        End Sub




    Sunday, April 10, 2016 12:02 AM

Answers

  • User409696431 posted

    Response.Write won't work in response to an asynch postback.  The information can't be parsed correctly without a full page load.

    If you want to test things, add a test label inside your UpdatePanel content, and instead of using Response.Write, just set the label text to the output you want to see.

    It will now work.

    You also don't need the Trigger statement, since the Calendar is inside the UpdatePanel.   The default is for an UpdatePanel to respond to events inside it with an asynch postback.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 10, 2016 2:17 AM
  • User61956409 posted
    
    

    Hi kengkit,

    kengkit

    my calendar is not working as expected if i placed it inside the UpdatePanel. (SelectionChanged is not firing)

    If you debug the code, you will find that Calendar SelectionChanged event could fire. But as KathyW said, Response Write() method won't work in response to an asynch postback. You could display the selected date with label control, or you could make Calendar work as a PostBack Trigger.

    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
        </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID="Calendar1" />
            <%--<asp:AsyncPostBackTrigger ControlID="Calendar1" EventName="SelectionChanged" />--%>
        </Triggers>
    </asp:UpdatePanel>
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 11, 2016 7:41 AM

All replies

  • User409696431 posted

    Response.Write won't work in response to an asynch postback.  The information can't be parsed correctly without a full page load.

    If you want to test things, add a test label inside your UpdatePanel content, and instead of using Response.Write, just set the label text to the output you want to see.

    It will now work.

    You also don't need the Trigger statement, since the Calendar is inside the UpdatePanel.   The default is for an UpdatePanel to respond to events inside it with an asynch postback.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 10, 2016 2:17 AM
  • User61956409 posted
    
    

    Hi kengkit,

    kengkit

    my calendar is not working as expected if i placed it inside the UpdatePanel. (SelectionChanged is not firing)

    If you debug the code, you will find that Calendar SelectionChanged event could fire. But as KathyW said, Response Write() method won't work in response to an asynch postback. You could display the selected date with label control, or you could make Calendar work as a PostBack Trigger.

    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
        </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID="Calendar1" />
            <%--<asp:AsyncPostBackTrigger ControlID="Calendar1" EventName="SelectionChanged" />--%>
        </Triggers>
    </asp:UpdatePanel>
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 11, 2016 7:41 AM