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

Using RadTimePicker Class for overriding SelectedDate Property

3 Answers 106 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
nitesh monga
Top achievements
Rank 1
nitesh monga asked on 07 Jul 2009, 08:11 PM
Hi there,

I am trying to create a custom class which inherits radtimepicker class, and overrides the SelectedDate property of the class. but i am stuck at how to use this class with the radtimepicker control and let that control use the override property.

why i want to do this is because we have lots for classic RadControls in our application now we are migrating them to the new ajax version, previously we were using asp:TextBox with JS to get the time value and to validate the value, with radtimepicker we get all the functionality we want without any extra JS :). the only problem is getting only the time value from the radtimepicker and not the date with that. while searching i found i can do that by using CType(radtimepicker .SelectedDate, DateTime).ToString("tt") but this will take a lot of time to implement :(. if i can just override the SelectedDate property and place the above .ToString("tt") with the GETTER it will be more easy :).

i hope i am able to explain my problem. any example code will be a great help.

Thanks
Nitesh Monga

3 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 10 Jul 2009, 11:03 AM
Hello nitesh,

DateTime object's ToShortTimeString() probably is exactly what you need. So something like:

radTimePicker.SelectedDate.Value.ToShortTimeString()  should work for you. If not, here is a sample approach:

public class CustomRadTimePicker : RadTimePicker 
    public string SelectedTime 
    { 
        get 
        { 
            return this.SelectedDate.Value.ToShortTimeString(); 
        } 
        set 
        { 
            DateTime date; 
            if (DateTime.TryParse(value, out date)) 
            { 
                this.SelectedDate = date; 
            } 
            else 
            { 
                throw new Exception("String cannot be parsed to a valid DateTime."); 
            } 
        } 
    } 

Public Class CustomRadTimePicker 
    Inherits RadTimePicker 
    Public Property SelectedTime() As String 
        Get 
            Return Me.SelectedDate.Value.ToShortTimeString() 
        End Get 
        Set 
            Dim [date] As DateTime 
            If DateTime.TryParse(value, [date]) Then 
                Me.SelectedDate = [date] 
            Else 
                Throw New Exception("String cannot be parsed to a valid DateTime."
            End If 
        End Set 
    End Property 
End Class 


Sincerely yours,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
nitesh monga
Top achievements
Rank 1
answered on 11 Jul 2009, 08:33 PM
Thank you for the sample code :). i already tested this code but i am unable to use this class with the rad controls :(. it always give a type cast exception..

thanks
Nitesh
0
Accepted
Veli
Telerik team
answered on 13 Jul 2009, 11:37 AM
Hello Nitesh,

What are trying to achieve? Can you share some sample code? Alternatively, try the ToShortTimeString() apporach.

Regards,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Ajax
Asked by
nitesh monga
Top achievements
Rank 1
Answers by
Veli
Telerik team
nitesh monga
Top achievements
Rank 1
Share this question
or