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

Set the SelectedDate to Null

4 Answers 445 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
Ric
Top achievements
Rank 1
Ric asked on 04 Feb 2011, 05:35 PM
Hi,

I have a little question,

In my application i have a RadDatePicker declared so :

<telerik:RadDatePicker id="myDate" runat="server" Skin="Default">
                <DateInput DateFormat="dd/MM/yyyy"></DateInput>
            </telerik:RadDatePicker>

And i want to make it nullable : for the moment the user can write the date in the inputControl manually , but if he wants to set the Date to Null it's not possible.

In my CodeBehind i have a button which update the date in a database, it takes the code the selectedDate to make this update.

        protected void lnkUpdateDate_Click(object sender, EventArgs e)
        {
              if(myDate.DateInput.SelectedValue != null)
                  Update((DateTime)myDate.DateInput.SelectedValue);
        }

I can't make the difference between an emtpy inputextbox.text value and wrong date in input textbox.text

How can I test if the User has enter an empty string in order to make the update with a null Date ?

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 07 Feb 2011, 08:46 AM
Hello Ric,


One suggestion to identify whether the DatePicker having Date or invalid date is by using HiddenField on page. Attach OnError client side event to RadDatePicker and update the HiddenField value in the handler. Now check for the HiddenField value in code behind to diferentiate between date is selected or invalid date typed.


-Shinu.
0
Ric
Top achievements
Rank 1
answered on 07 Feb 2011, 02:16 PM
Ok client side i have now this :

                    function ClientDateError(sender, args)
                     {
                         var hdnlbl = document.getElementById("hdnInputDateValue")   // my hidden label
                     }

How can I put the value from the input RadDatePicker in my hidden label ?

(I'm not good in js )
0
Shinu
Top achievements
Rank 2
answered on 08 Feb 2011, 12:49 PM
Hello Ric,

Here is a sample code to achieve your scenario.

JavaScript:
    function OnError(sender, eventArgs)
     {
        var hdnlbl = document.getElementById("hdnInputDateValue")
        hdnlbl.value = 1;//setting the hiddenFieldValue
     }

CodeBehind:
protected void Button1_Click(object sender, EventArgs e)
   {
       if (DatePicker1.SelectedDate == null && hdnInputDateValue.Value == "0")
       {
       //gets into the loop only if the user leaves the date unselected.
       }
   }

aspx:
<telerik:RadDatePicker ID="DatePicker1" runat="server">
   <DateInput DateFormat="dd MMM yyyy" ClientEvents-OnError="OnError">
   </DateInput>
</telerik:RadDatePicker>
<asp:HiddenField ID="hdnInputDateValue" runat="server"  Value="0"/>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

Thanks,
Shinu.
0
Ric
Top achievements
Rank 1
answered on 08 Feb 2011, 01:37 PM
Ok thanks , but in this code I can't make the difference between an empty string (considered as error) and a real error
I have 3 cases and in my code behind I will do that :

protected void Button1_Click(object sender, EventArgs e)
   {
       if (DatePicker1.SelectedDate != null)   // If Selected Date is good format I update the date
       {
             UpdateDate(DatePicker1.SelectedDate);
       }
      else if( string.IsNullOrEmpty(hdnDateInputValue.Text))  // If the input text value is empty I update the date with null
      {
            UpdateDate(null);
      }
     else   // when  the input text is not empty , and selected date is null than it's a bad format and i don't update
     {
     // Nothing , maybe I give a warning
     }
   }


Because at the form load , the Control RadDatePicker can have a selected Date. If the user delete the inputvalue it's considered like an error and so I can't update my field with null like in my sample
Tags
Calendar
Asked by
Ric
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Ric
Top achievements
Rank 1
Share this question
or