Context menu breaking my application

4 posts, 0 answers
  1. Alan
    Alan avatar
    107 posts
    Member since:
    Oct 2010

    Posted 04 Feb 2011 Link to this post

    Hi,

    I have some code which displays timings in the allday row, specifically this one. My Code is:

    protected void radScheduler_TimeSlotCreated(object sender, TimeSlotCreatedEventArgs e)
    {
        // Calculate total hours per day.
        if (radScheduler.SelectedView == SchedulerViewType.WeekView && e.TimeSlot.Duration.TotalDays == 1)
        {
            double totalHoursPerDay         = 0;
            double totalOvertimeHoursPerDay = 0;
            bool weekend = false;
     
            if (e.TimeSlot.Start.DayOfWeek == DayOfWeek.Saturday || e.TimeSlot.Start.DayOfWeek == DayOfWeek.Sunday)
            {
                weekend = true;
            }
     
            foreach (Appointment a in radScheduler.Appointments.GetAppointmentsInRange(e.TimeSlot.Start, e.TimeSlot.End))
            {
                // Check time of appointment and adding to hours or overtime accordingly.
                if (a.Start.TimeOfDay >= new TimeSpan(8, 0, 0) && a.End.TimeOfDay <= new TimeSpan(19, 0, 0) && weekend)
                {
                    totalOvertimeHoursPerDay += a.Duration.TotalHours;
                }
                else if (a.Start.TimeOfDay >= new TimeSpan(8, 0, 0) && a.End.TimeOfDay <= newTimeSpan(17, 0, 0))
                {
                    totalHoursPerDay += a.Duration.TotalHours;
                }
                else if (a.Start.TimeOfDay > new TimeSpan(17, 0, 0) && a.End.TimeOfDay <= newTimeSpan(19, 0, 0))
                {
                    totalOvertimeHoursPerDay += a.Duration.TotalHours;
                }
                else if (a.Start.TimeOfDay >= new TimeSpan(8, 0, 0) && a.End.TimeOfDay <= newTimeSpan(19, 0, 0))
                {
                    double overtime = a.End.TimeOfDay.TotalHours - 17;
                    totalOvertimeHoursPerDay += overtime;
                    totalHoursPerDay += (a.Duration.TotalHours - overtime);
                }
            }
     
            Literal hoursLiteral       = new Literal();
            Literal overtimeLiteral    = new Literal();
     
            if (!weekend)
            {
                hoursLiteral.Text = "Hrs: " + totalHoursPerDay.ToString();
            }
     
            overtimeLiteral.Text = " O/T: " + totalOvertimeHoursPerDay.ToString();
     
            radScheduler.Controls.Add(hoursLiteral);
            radScheduler.Controls.Add(overtimeLiteral);
     
            e.TimeSlot.Control.Controls.Add(hoursLiteral);
            e.TimeSlot.Control.Controls.Add(overtimeLiteral);
        }
    }

    With the context menu enabled, this is causing an exception when I switch week (in weekview). The exception is: "Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.", and it happens on:

    radScheduler.Controls.Add(hoursLiteral);

    You can find a sample code here.

    Thank you for your support.

    Daryl
  2. Alan
    Alan avatar
    107 posts
    Member since:
    Oct 2010

    Posted 07 Feb 2011 Link to this post

    Forgot to add scheduler declaration code.

    <telerik:RadScheduler ID="radScheduler" runat="server" DayEndTime="19:00:00" FirstDayOfWeek="Monday"
        LastDayOfWeek="Sunday" OverflowBehavior="Expand" SelectedView="WeekView" ShowAllDayRow="True"
        ShowFooter="False" Skin="Windows7" WorkDayEndTime="19:00:00" OnClientAppointmentClick="OnClientAppointmentClick"
        OnClientTimeSlotClick="OnClientTimeSlotClick" ShowViewTabs="False" CustomAttributeNames="Task, FileCode, DocketNo"
        AdvancedForm-EnableCustomAttributeEditing="true" AllowInsert="False" OnTimeSlotCreated="radScheduler_TimeSlotCreated"
        OnFormCreated="radScheduler_FormCreated" StartInsertingInAdvancedForm="false"
        EnableDescriptionField="true" EnableCustomAttributeEditing="true" Localization-AdvancedSubject="Code Center"
        OnAppointmentContextMenuItemClicked="radScheduler_AppointmentContextMenuItemClicked"
        EnableExactTimeRendering="true" OnNavigationComplete="radScheduler_NavigationComplete">
        <AdvancedForm Modal="True" />
        <Localization AdvancedSubject="Code Center"></Localization>
        <TimelineView UserSelectable="False" />
        <MonthView UserSelectable="False" />
        <AppointmentContextMenus>
            <telerik:RadSchedulerContextMenu ID="contextMenu" runat="server">
                <Items>
                    <telerik:RadMenuItem runat="server" Text="Negate Timesheet" Value="NegateCommand">
                    </telerik:RadMenuItem>
                </Items>
            </telerik:RadSchedulerContextMenu>
        </AppointmentContextMenus>
        <AppointmentContextMenuSettings Skin="Windows7" />
    </telerik:RadScheduler>
  3. Peter
    Admin
    Peter avatar
    6637 posts

    Posted 07 Feb 2011 Link to this post

    Hi Daryl,

    Thank you for providing a working demo of the issue.

    I wasn't able to find out exactly why this problem occurs, but you can workaround it by setting EnableViewState="false" for RadScheduler and setting the data binding properties in Page_Init instead of Page_Load:.

    protected void Page_Init(object sender, EventArgs e)
        {
            List<Event> eventsList = new List<Event>();
              
            //Event events = new Event();
            //events.eventID = 1;
            //events.start = DateTime.Today.AddHours(9);
            //events.end = DateTime.Today.AddHours(12);
            //events.subject = "Test";
            //events.description = "Test Description";
            //eventsList.Add(events);
            radScheduler.DataSource = eventsList;
            radScheduler.DataKeyField = "eventID";
            radScheduler.DataStartField = "start";
            radScheduler.DataEndField = "end";
            radScheduler.DataDescriptionField = "descritpion";
            radScheduler.DataSubjectField = "subject";
        }

    I will log this problem and we will investigate further, but in the meantime I hope the above solution works well for your case.


    Kind regards,
    Peter
    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.
  4. Alan
    Alan avatar
    107 posts
    Member since:
    Oct 2010

    Posted 07 Feb 2011 Link to this post

    Thanks Peter,

    That was just a demo, the real application is getting data from the database. Which means it's not ideal to query the database every time there's a postback (since there's no caching implemented yet). For now I'm going to leave it that way, since theres not much data yet.

    I had to modify it a bit, instead it's doing like this:

    protected void Page_Init(object sender, EventArgs e)
    {
        radScheduler.DataKeyField = "ID";
        radScheduler.DataStartField = "From";
        radScheduler.DataEndField = "To";
        radScheduler.DataDescriptionField = "Description";
        radScheduler.DataSubjectField = "CostCenter";
    }

    Instead setting the datasource in page load, because for some reason other viewstate variables were losing value.

    Many thanks for your consistent help,
    Daryl
Back to Top