Telerik RadScheduler - Getting the selected range from the serverside?

When you select a range of times in the RadScheduler control and right click, you get the TimeSlot context menu - but the event raised when you choose an item from this menu only has one timeslot that has a duration of the smallest period of time that you can select in the current view (Week, Day, Month).

So how can I get the selected time range on right-click on the serverside?

1

1 Answer

Unfortunately there appears to be a bug in the RadScheduler and its handling of the selected timeslots when they're sent server-side - as you've noticed, only one of the timeslots is sent. This is documented in Telerik's Public Issue Tracker here. The bug has been around a while and probably hasn't been addressed yet because so few people seem to care - it's only gotten three votes, so get over there and vote.

The good news is that there's a workaround. I'd mentioned it in my comment and thought I'd take a further look. You can trigger a custom Ajax request from the client side when the user selects the range of timeslots, right-clicks, and triggers your client-side handler.

Here's the relevant code in your aspx page, with the Javascript function for the RadScheduler control to use for the OnClientTimeSlotContextMenuItemClicking event. Note the RadAjaxManager object has a custom handler for its OnAjaxRequest property; this will have to be defined in the codebehind. Also, I found it necessary to include the Javascript inside a RadCodeBlock, as the function is accessing the RadAjaxManager object.

<telerik:RadAjaxManager runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest">
</telerik:RadAjaxManager>
<telerik:RadCodeBlock runat="server"> <script type="text/javascript"> function OnClientTimeSlotContextMenuItemClicking(sender, args) { var selectedSlots = sender.get_selectedSlots(); var firstSlotFromSelection = selectedSlots[0]; var lastSlotFromSelection = selectedSlots[selectedSlots.length - 1]; var customArgs = "TimeSlotMenuItemClicked," + firstSlotFromSelection.get_endTime() + "," + lastSlotFromSelection.get_endTime(); // for testing purposes... // alert(customArgs); $find("<%= RadAjaxManager1.ClientID %>").ajaxRequest(customArgs); } </script>
</telerik:RadCodeBlock>

What I'm doing with the customArgs field in the Javascript code there is putting together all the data we'll need on the server side, as there's only one argument allowed; the workaround for this is to assemble the data we need into an object in some suitable format that we can successfully parse when it arrives in the server method. Telerik mentions this workaround here if you're interested, but I've also seen this kind of technique in other places.

Here's the OnAjaxRequest handler I used in my codebehind. I put together a separate method to parse the date/time out of the string and get the timezone right.

protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
{ String[] argArray = e.Argument.Split(",".ToCharArray()); if (argArray.Length > 2 && argArray[0] == "TimeSlotMenuItemClicked") { DateTime dtStart = GetDateTimeFromArgument(argArray[1]); DateTime dtEnd = GetDateTimeFromArgument(argArray[2]); // Starting date/time is the end of the first timeslot; adjust to arrive at the beginning TimeSpan tsSlotLength = new TimeSpan(0, RadScheduler1.MinutesPerRow, 0); dtStart -= tsSlotLength; // Do what we need to do with the start/end now }
}
/// <summary>
/// Date/Time format will look like this: "Sat Apr 06 2013 10:30:00 GMT-0700 (US Mountain Standard Time)"
/// Turn this from a string into a date.
/// </summary>
/// <param name="arg"></param>
/// <returns></returns>
private DateTime GetDateTimeFromArgument(string arg)
{ // Extract the timezone qualifier and put together a string we can parse. string formattedArg = string.Format("{0} {1}:{2}", arg.Substring(0, 24), arg.Substring(28, 3), arg.Substring(31, 2)); return DateTime.ParseExact(formattedArg, "ddd MMM dd yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture);
}
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like