RadDatePicker keyboard shortcut to pick today's date

6 posts, 0 answers
  1. Richard
    Richard avatar
    2 posts
    Member since:
    Jun 2009

    Posted 24 Jun 2011 Link to this post

    Is it possible to specify a keyboard shortcut so that if a user presses "T", rather than "T" appearing in the input box, it instead selects today's date?
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 24 Jun 2011 Link to this post

    Hello Richard,

    You can achieve this attaching the client event OnKeyPress. Here is the sample code.
    aspx:
    <telerik:RadDatePicker ID="DatePicker1" runat="server">
           <DateInput ClientEvents-OnKeyPress="OnKeyPress" ></DateInput>
    </telerik:RadDatePicker>

    Clientside:
    function OnKeyPress(Sender, args)
      {
            if (args.get_keyCharacter() == "T")
           {
                var datePicker = $find("<%= DatePicker1.ClientID %>");
                var date = new Date();
                date.setDate(date.getDate());
                datePicker.set_selectedDate(date);
            }
      }

    Thanks,
    Shinu.
  3. Richard
    Richard avatar
    2 posts
    Member since:
    Jun 2009

    Posted 24 Jun 2011 Link to this post

    Hi Shinu,

    Thanks for the rapid answer! This solutions works great for controls that I've added via the aspx page, but how would I do it for dynamically created ones?

    Thanks again,

    R.
  4. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 29 Jun 2011 Link to this post

    Hi Richard,

    Modify the javascript as below and it would work for dynamically created pickers as well:
    function OnKeyPress(Sender, args) {
        if (args.get_keyCharacter() == "T"){
            var datePicker = sender;
            var date = new Date();
            date.setDate(date.getDate());
            datePicker.set_selectedDate(date);
        }
    }



    Regards,
    Iana
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  5. Patrick
    Patrick avatar
    1 posts
    Member since:
    Jun 2012

    Posted 28 Jun 2012 Link to this post

    I've implemented this code and it sets the time perfectly, but the "T" still remains in the textbox either before of after full date. If I place an alert in the js to view the contents of the textbox, the "T" does not appear. I'm using version v4.0.30319.

    Here is the "working" code :
    // works, places "6/28/2012 10:54 AM" in the textbox
    function OnKeyPress(Sender, args) {
        if (args.get_keyCharacter() == "T" || args.get_keyCharacter() == "t") {
            var datePicker = $find("<%= dtpStartDateTime.ClientID %>");
            var date = new Date();
            date.setDate(date.getDate());
            datePicker.set_selectedDate(date);
            alert(datePicker.get_textBox().value);
        }
    }

    Here is the non-working code:
    // does not work, places "T6/28/2012 10:54 AM" or "6/28/2012 10:54 AMT" in the textbox
     function OnKeyPress(Sender, args) {
         if (args.get_keyCharacter() == "T" || args.get_keyCharacter() == "t") {
             var datePicker = $find("<%= dtpStartDateTime.ClientID %>");
             var date = new Date();
             date.setDate(date.getDate());
             datePicker.set_selectedDate(date);
         }
     }

    I have also tried placing arbitrary text in the textbox, if I do not include an alert, it always places the T before or after the arbitrary text.

    Thanks for any help.
  6. Vasil
    Admin
    Vasil avatar
    1633 posts

    Posted 29 Jun 2012 Link to this post

    Hi Patrick,

    Try to cancel the keypress event using args.set_cancel(true);

    Regards,
    Vasil
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top