locked
Calender post back while selecting different month in C#.NET RRS feed

  • Question

  • User-1578974752 posted

    Hi

    I am using the calender in c#.net.

    below is the code. My issue is :

    When ever I changing the month arrow of the calender, the postback is happening and form is jumping just like if you cick a button . user don't want that. How can I avoid this

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

    {

    Calendar1.Visible = true;

    // Calendar1.Attributes.Add("style", "");

    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)

    {

    frmtxt.Text = Calendar1.SelectedDate.ToShortDateString();

    //frmtxt.Text = Calendar1.SelectedDate.ToLongDateString();

    Label4.Text = Convert.ToDateTime(frmtxt.Text).ToString("dd-MMM-yyyy", CultureInfo.InvariantCulture);

    frmtxt.Text = Label4.Text;

    Calendar1.Visible = false;

    }

    Thanks

    Wednesday, April 28, 2021 3:16 AM

Answers

  • User-939850651 posted

    Hi shsu,

    shsu

    When ever I changing the month arrow of the calender, the postback is happening and form is jumping just like if you cick a button . user don't want that. How can I avoid this

    Have you tried to put the Calender control inside the UpdatePanel? This can avoid the refresh of the entire page.

    Something like this:

        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager ID="ScriptManagerCalendar" runat="server">
                </asp:ScriptManager>
                <asp:UpdatePanel ID="UpdatePanelCalendar" runat="server">
                    <ContentTemplate>
                        <asp:Label ID="LabelDate" runat="server" Text="Date:"></asp:Label>
                        <asp:TextBox ID="TextBoxDate" runat="server" Font-Size="Small"
                            Height="10" ReadOnly="true" Width="80px"></asp:TextBox>
                        <asp:ImageButton ID="ImageButtonCalendar" runat="server"
                            ImageUrl="~/img/calender.png" Width="15" Height="15"
                            OnClick="ImageButtonCalendar_Click" />
    
                        <asp:Calendar ID="Calendar1" runat="server"
                            Visible="false"
                            OnSelectionChanged="Calendar1_SelectionChanged">
                            <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
                            <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
                            <OtherMonthDayStyle ForeColor="#CC9966" />
                            <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
                            <SelectorStyle BackColor="#FFCC66" />
                            <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
                            <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
                        </asp:Calendar>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>
        </form>
            protected void ImageButtonCalendar_Click(object sender, ImageClickEventArgs e)
            {
                Calendar1.Visible = true;
            }
    
            protected void Calendar1_SelectionChanged(object sender, EventArgs e)
            {
                DateTime dt = Calendar1.SelectedDate;
                TextBoxDate.Text = dt.ToString("dd/MM/yyyy");
                Calendar1.Visible = false;
            }

    Hope this can help you.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 28, 2021 8:54 AM

All replies

  • User-1545767719 posted

    I suggest that you consider the following alternatives:

    Calendar Demonstration
    https://ajaxcontroltoolkit.devexpress.com/Calendar/Calendar.aspx

    Datepicker
    https://jqueryui.com/datepicker/

    Wednesday, April 28, 2021 6:10 AM
  • User-939850651 posted

    Hi shsu,

    shsu

    When ever I changing the month arrow of the calender, the postback is happening and form is jumping just like if you cick a button . user don't want that. How can I avoid this

    Have you tried to put the Calender control inside the UpdatePanel? This can avoid the refresh of the entire page.

    Something like this:

        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager ID="ScriptManagerCalendar" runat="server">
                </asp:ScriptManager>
                <asp:UpdatePanel ID="UpdatePanelCalendar" runat="server">
                    <ContentTemplate>
                        <asp:Label ID="LabelDate" runat="server" Text="Date:"></asp:Label>
                        <asp:TextBox ID="TextBoxDate" runat="server" Font-Size="Small"
                            Height="10" ReadOnly="true" Width="80px"></asp:TextBox>
                        <asp:ImageButton ID="ImageButtonCalendar" runat="server"
                            ImageUrl="~/img/calender.png" Width="15" Height="15"
                            OnClick="ImageButtonCalendar_Click" />
    
                        <asp:Calendar ID="Calendar1" runat="server"
                            Visible="false"
                            OnSelectionChanged="Calendar1_SelectionChanged">
                            <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
                            <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
                            <OtherMonthDayStyle ForeColor="#CC9966" />
                            <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
                            <SelectorStyle BackColor="#FFCC66" />
                            <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
                            <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
                        </asp:Calendar>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>
        </form>
            protected void ImageButtonCalendar_Click(object sender, ImageClickEventArgs e)
            {
                Calendar1.Visible = true;
            }
    
            protected void Calendar1_SelectionChanged(object sender, EventArgs e)
            {
                DateTime dt = Calendar1.SelectedDate;
                TextBoxDate.Text = dt.ToString("dd/MM/yyyy");
                Calendar1.Visible = false;
            }

    Hope this can help you.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 28, 2021 8:54 AM