protected void okBtn_Click(object sender, System.EventArgs e)
        {
            try
            {
                Task task = GetTask();
                bool isNewTask = (task == null);

                if (isNewTask)
                    task = new Task();

                task.Name = this.taskNameEdit.Text;
                task.Pattern = new RecurrencePattern(this.iCalendarText.Text);

                //Test drive the pattern to make sure it is valid.
                task.Pattern.GenerateOccurrences(DateTime.Now, DateTime.Now);

                if (isNewTask)
                    model.AddTask(task);

                Response.Redirect(ViewState["referrer"].ToString());
            }
            catch (Exception ex)
            {
                validationLabel.Text = ex.Message;
                validationLabel.Visible = true;
            }
        }
        /// <summary>
        /// Called when the user clicks okay. Performs validation and saves the data either into a new 
        /// task or into task we are editing.
        /// </summary>
        protected void okBtn_Click(object sender, System.EventArgs e)
        {
            try
            {
                Task task = GetTask();
                bool isNewTask = (task == null);

                if (isNewTask)
                    task = new Task();

                EditTaskController controller = new EditTaskController();
                controller.Save(GetTaskData(), task);

                if (isNewTask)
                    this.model.AddTask(task);

                Response.Redirect(ViewState["referrer"].ToString());
            }
            catch (ValidateException ex)
            {
                validationLabel.Text = ex.Message;
                validationLabel.Visible = true;
            }
        }
 private void UpdateiCalendar()
 {
     try
     {
         Task dummyTask = new Task();
         EditTaskController controller = new EditTaskController();
         controller.Save(GetTaskData(), dummyTask);
         iCalendarText.Text = dummyTask.Pattern.ToiCalendar();
     }
     catch (Exception ex)
     {
         this.iCalendarText.Text = ex.Message;
     }
 }
 /// <summary>
 /// Adds a new task to the model and notifies the views.
 /// </summary>
 public void AddTask(Task task)
 {
     mTasks.Add(task.Id, task);
     OnTaskChanged();
 }
        /// <summary>
        /// Saves data from the data set into a Task object.
        /// </summary>
        public void Save(EditTaskFormData.TaskRow taskData, Task task)
        {
            mTaskData = taskData;
            mTask = task;

            Validate();

            //Save task name
            mTask.Name = mTaskData.Name;

            //Create a new recurrence pattern that we will save the data into.
            mTask.Pattern = new RecurrencePattern();
            mRule = mTask.Pattern.RRules.Add();

            //Save the recurrence rule
            switch ((RecurrenceChoice)mTaskData.RecurrenceChoice)
            {
                case RecurrenceChoice.Daily:
                    SaveDaily();
                    break;
                case RecurrenceChoice.Weekly:
                    SaveWeekly();
                    break;
                case RecurrenceChoice.Monthly:
                    SaveMonthly();
                    break;
                case RecurrenceChoice.Yearly:
                    SaveYearly();
                    break;
                default:
                    throw new Exception("Unknown recurrence choice.");
            }

            //Save the range of the pattern
            mTask.Pattern.StartDate = DateTime.Parse(mTaskData.StartDate) + TimeSpan.Parse(mTaskData.StartTime);
            switch ((EndType)mTaskData.EndChoice)
            {
                case EndType.None:
                    mRule.EndType = EndType.None;
                    break;
                case EndType.Count:
                    mRule.Count = int.Parse(mTaskData.Count);
                    break;
                case EndType.Until:
                    mRule.Until = DateTime.Parse(mTaskData.EndDate);
                    break;
                default:
                    throw new Exception("Unknown end type.");
            }
        }
        /// <summary>
        /// Transfers information from a Task object into a data row.
        /// </summary>
        public EditTaskFormData.TaskRow LoadTask(Task task)
        {
            PatternParser parser = new PatternParser();
            if (!parser.Parse(task.Pattern))
                throw new Exception("This is not a simple pattern.");

            mTaskData = NewTask();
            mTask = task;
            mRule = mTask.Pattern.RRules[0];

            //Load task name
            mTaskData.Name = task.Name;

            //Load the recurrence rule
            switch (parser.PatternType)
            {
                case PatternType.Daily:
                    LoadDaily();
                    break;
                case PatternType.Weekly:
                    LoadWeekly();
                    break;
                case PatternType.MonthlyDay:
                    LoadMonthlyDay();
                    break;
                case PatternType.MonthlyDayOfWeek:
                    LoadMonthlyDayOfWeek();
                    break;
                case PatternType.MonthlyWeekDay:
                    LoadMonthlyWeekDay();
                    break;
                case PatternType.YearlyDay:
                    LoadYearlyDay();
                    break;
                case PatternType.YearlyDayOfWeek:
                    LoadYearlyDayOfWeek();
                    break;
                case PatternType.YearlyWeekDay:
                    LoadYearlyWeekDay();
                    break;
                default:
                    throw new Exception("Unknown pattern type.");
            }

            //Load the range of the pattenr
            mTaskData.StartDate = DemoUtil.FormatDate(task.Pattern.StartDate);
            mTaskData.StartTime = DemoUtil.FormatTime(task.Pattern.StartDate);
            mTaskData.EndChoice = (int)mRule.EndType;
            switch (mRule.EndType)
            {
                case EndType.None:
                    //Do nothing
                    break;
                case EndType.Count:
                    mTaskData.Count = mRule.Count.ToString();
                    break;
                case EndType.Until:
                    mTaskData.EndDate = DemoUtil.FormatDate(mRule.Until);
                    break;
                default:
                    throw new Exception("Unknown end type.");
            }

            return mTaskData;
        }