1

当您在 RadScheduler 控件中选择时间范围并右键单击时,您将获得 TimeSlot 上下文菜单 - 但是当您从该菜单中选择一个项目时引发的事件只有一个时间段,其持续时间是您可以在当前视图中选择(周、日、月)。

那么如何在服务器端右键单击获得选定的时间范围?

4

1 回答 1

2

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 ID="RadAjaxManager1" runat="server"
    OnAjaxRequest="RadAjaxManager1_AjaxRequest">
</telerik:RadAjaxManager>

<telerik:RadCodeBlock ID="RadCodeBlock1" 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);
}
于 2013-04-07T04:59:39.807 回答