Setting the SelectedDate property of the RadDatePicker disables the SelectedDateChanged event handler?

5 posts, 2 answers
  1. Boris
    Boris avatar
    288 posts
    Member since:
    Oct 2012

    Posted 29 Jul 2014 Link to this post

    I have a RadDateTimePicker on a page of my C# DotNet 4.0 web app.  
    The idea is that every time the selected date changes, the event handler updates a text box. 
    This is intended to be an Ajax event, not a full page postback.
    To this end, the controls are set up in the RadAjaxManager as follows:

    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" >      
            <AjaxSettings>            
                <telerik:AjaxSetting AjaxControlID="rdtDateTime">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="txtCount" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>        
        </telerik:RadAjaxManager>

    This works perfectly, unless in the page load event I set the SelectedDate property of the RadDateTimePicker.
    If I do that the SelectedDateChanged event stops firing. 

    Suggestions?  
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 30 Jul 2014 in reply to Boris Link to this post

    Hi Boris,

    Please have a look into the following code I tried which works fine at my end.

    ASPX:
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RLoadingPanel">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="rdtDateTime">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="txtCount" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RLoadingPanel" runat="server" Skin="Web20">
    </telerik:RadAjaxLoadingPanel>
    <div>
        <telerik:RadDateTimePicker ID="rdtDateTime" runat="server" OnSelectedDateChanged="rdtDateTime_SelectedDateChanged"
            AutoPostBackControl="Both">
        </telerik:RadDateTimePicker>
        <telerik:RadTextBox ID="txtCount" runat="server">
        </telerik:RadTextBox>
    </div>

    C#:
    protected void rdtDateTime_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
    {
        txtCount.Text = rdtDateTime.SelectedDate.ToString();
    }

    Please get back with your complete code if this doesn't help.

    Thanks,
    Princy.
  3. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 30 Jul 2014 in reply to Boris Link to this post

    Hi Boris,

    I made some changes in the C# code and here is the updated code.

    C#:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Setting a date dyanamically during the page load.
            rdtDateTime.SelectedDate = DateTime.Now;
        }
    }
    protected void rdtDateTime_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
    {
        txtCount.Text = rdtDateTime.SelectedDate.ToString();
    }

    Thanks,
    Princy.
  4. Boris
    Boris avatar
    288 posts
    Member since:
    Oct 2012

    Posted 30 Jul 2014 Link to this post

    Painfully obvious now.  

    The code to set the SelectedDate was in a function that needed to be called on all Page_Loads, not just !IsPostBack.

    Thanks.
  5. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 31 Jul 2014 in reply to Boris Link to this post

    Hi Boris,

    When you select a date from the RadDateTimePicker with AutoPostBackControl property set, the Page_Load event fires before the SelectedDateChanged event which is always expected.

    So if you are setting the SelectedDate property in the Page_Load (outside the !IsPostBack), each time you select a date, the Page_Load will fire and the date you selected from the RadDateTimePicker UI gets overridden with the value you are setting in the page load event and as a result the same gets reflected in the SelectedDateChanged event as well. Hence the user have a feeling like the date selection is not working and setting the SelectedDate property directly in the Page_Load is not advisable.

    Thanks,
    Princy.
Back to Top