Set the SelectedDate to Null

5 posts, 0 answers
  1. Ric
    Ric avatar
    3 posts
    Member since:
    Feb 2011

    Posted 04 Feb 2011 Link to this post

    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 ?
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 07 Feb 2011 Link to this post

    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.
  3. Ric
    Ric avatar
    3 posts
    Member since:
    Feb 2011

    Posted 07 Feb 2011 Link to this post

    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 )
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 08 Feb 2011 Link to this post

    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.
  5. Ric
    Ric avatar
    3 posts
    Member since:
    Feb 2011

    Posted 08 Feb 2011 Link to this post

    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
Back to Top