【问题标题】:Telerik RadScheduler - Getting the selected range from the serverside?Telerik RadScheduler - 从服务器端获取选定的范围?
【发布时间】:2013-03-27 11:39:20
【问题描述】:

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

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

【问题讨论】:

  • 你见过this thread吗?推荐的方法是在客户端捕获所选范围的开始/结束,然后通过 Ajax 请求将它们传递给服务器。

标签: c# .net telerik server-side


【解决方案1】:

不幸的是,在 RadScheduler 中似乎存在一个错误,它在将所选时间段发送到服务器端时对其进行处理 - 正如您所注意到的,只发送了一个时间段。这在 Telerik 的公共问题跟踪器here 中有记录。这个错误已经存在了一段时间,可能还没有得到解决,因为似乎很少有人关心它——它只获得了三票,所以过来投票吧。

好消息是有一种解决方法。我在评论中提到了它,并认为我会进一步研究。当用户选择时隙范围、右键单击并触发您的客户端处理程序时,您可以从客户端触发自定义 Ajax 请求。

这是您的 aspx 页面中的相关代码,其中包含用于 RadScheduler 控件的 Javascript 函数以用于 OnClientTimeSlotContextMenuItemClicking 事件。注意 RadAjaxManager 对象的 OnAjaxRequest 属性有一个自定义处理程序;这必须在代码隐藏中定义。此外,我发现有必要在 RadCodeBlock 中包含 Javascript,因为该函数正在访问 RadAjaxManager 对象。

<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>

我对 Javascript 代码中的 customArgs 字段所做的事情是将我们在服务器端需要的所有数据放在一起,因为只允许一个参数;解决方法是将我们需要的数据以某种合适的格式组装到一个对象中,当它到达服务器方法时,我们可以成功地对其进行解析。 Telerik 提到了这种解决方法here,如果您有兴趣,但我在其他地方也看到过这种技术。

这是我在代码隐藏中使用的 OnAjaxRequest 处理程序。我整理了一个单独的方法来解析字符串中的日期/时间并获得正确的时区。

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);
}

【讨论】:

    猜你喜欢
    • 2011-11-13
    • 2016-05-10
    • 2013-08-24
    • 1970-01-01
    • 2011-11-21
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    相关资源
    最近更新 更多