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

RadDatePicker keyboard shortcut to pick today's date

5 Answers 161 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 24 Jun 2011, 10:50 AM
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?

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 24 Jun 2011, 11:28 AM
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.
0
Richard
Top achievements
Rank 1
answered on 24 Jun 2011, 02:26 PM
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.
0
Iana Tsolova
Telerik team
answered on 29 Jun 2011, 08:04 AM
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.

0
Patrick
Top achievements
Rank 1
answered on 28 Jun 2012, 02:55 PM
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.
0
Vasil
Telerik team
answered on 29 Jun 2012, 02:55 PM
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.
Tags
General Discussions
Asked by
Richard
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Richard
Top achievements
Rank 1
Iana Tsolova
Telerik team
Patrick
Top achievements
Rank 1
Vasil
Telerik team
Share this question
or