This is a migrated thread and some comments may be shown as answers.

RadDatePicker DateChanged issue

1 Answer 176 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
nav100
Top achievements
Rank 1
nav100 asked on 03 May 2013, 05:42 PM

I have RadDatePicker control with SelectedDateChanged event. When I change the Date the event fires with Confirm window. When I click on 'Cancel' button RadDatePicker1_SelectedDateChanged invokes again and it displays Confirm window twice. When I click on 'OK' button it works fine. What could be the problem? Thanks for any suggestions. I am using 2010 Q3 controls.
                       

      <telerik:RadDatePicker ID="RadDatePicker1" runat="server" AutoPostBack="true" OnSelectedDateChanged="RadDatePicker1_SelectedDateChanged" >
                             </telerik:RadDatePicker>
   
   
    protected void RadDatePicker1_SelectedDateChanged(object sender, EventArgs e)
    {
    string radalertscript = "<script language='javascript'>function f(){ radconfirm('Are you sure?', confirmChange, 400, 100) ; Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";
                         Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert33", radalertscript);
     }

Here is Javascript function

    function confirmChange(args) {
        if (args) {
            __doPostBack("<%= hiddenButton1.UniqueID %>", "");
        }
    { var picker = $find("<%= rdpIndictment.ClientID %>");

      var hdval = document.getElementById('hiddenControlOldValue').value;

      picker.set_selectedDate(new Date(hdval));
   }
}
It looks like the problem is setting old value to Date Picker when 'Cancel' button clicked. Is there are any way I can Cancel Date Picker new selected value when 'Cancel' button clicked?

 

 

1 Answer, 1 is accepted

Sort by
0
Keith
Top achievements
Rank 2
answered on 04 May 2013, 09:24 PM
You are correct. The issue is that clicking the cancel button changes the value back to the original value, thus firing the event twice.

There are a couple of ways to handle this, but setting a flag seems pretty simple and straightforward.

<!-- Note: AutoPostBack may not be needed -->
<
telerik:RadDatePicker ID="RadDatePicker1" runat="server" AutoPostBack="false">
    <DateInput runat="server">
        <ClientEvents OnValueChanged="confirmChange" />
    </DateInput>
</telerik:RadDatePicker>

<script type="text/javascript" language="javascript">
    //Save old date value
    var hdval;
    //Set the flag to false
    var resetFlag = false;
    function valChanged(sender, args) {
        //No need to find a hidden control, args contains _oldValue and will suit our needs
        var hdval = args._oldValue;
        //If we are not resetting the control then fire the confirm dialog
        if (!resetFlag)
            radconfirm('Are you sure?', confirmChangeCallback, 400, 100);
        //Clear the reset flag
        resetFlag = false;
    }
    function confirmChangeCallback(args) {
        //Get the control to reset
        var picker = $find("<%= rdpIndictment.ClientID %>");
        if (args) {
            //Do stuff here on OK
            __doPostBack("<%= hiddenButton1.UniqueID %>", "");
        } else {
            //Do stuff here on cancel
            //Tell the OnValueChanged handler to ignore the next value change
            resetFlag = true;
            //Reset the date to the saved value
            picker.set_selectedDate(new Date(hdval));
        }
    }
</script>


Is that what you were looking for?
Tags
Calendar
Asked by
nav100
Top achievements
Rank 1
Answers by
Keith
Top achievements
Rank 2
Share this question
or