protected void Schedule_AjaxRequest(object sender, AjaxRequestEventArgs e) { List <DLL.Appointment> appointments = new List <DLL.Appointment>(); if (e.Argument == "RefreshScheduler") { bool opponentAvailabilitySet = false; if (playersRadGrid.SelectedItems.Count > 0) { int opponentId = Convert.ToInt32(playersRadGrid.SelectedItems[0].OwnerTableView.DataKeyValues[playersRadGrid.SelectedItems[0].ItemIndex]["UserId"]); appointments = GetAppointments(StoredData.User.UserId, opponentId, out opponentAvailabilitySet); } else { appointments = GetAppointments(StoredData.User.UserId, -1, out opponentAvailabilitySet); } } else if (e.Argument == "DeleteSchedule") { if (ViewState["DeletedAppointment"] != null) { Telerik.Web.UI.Appointment deletedAppointment = (Telerik.Web.UI.Appointment)ViewState["DeletedAppointment"]; using (FlexibleTennisLeagueDataContext dataContext = new FlexibleTennisLeagueDataContext()) { //Find match by the deleted appointment id List <SelectMatchByAppointmentIdResult> selectMatchByAppointmentIdResult = dataContext.SelectMatchByAppointmentId(Convert.ToInt32(deletedAppointment.ID)).ToList(); int?matchId = null; //If there are matches by the appointment id if (selectMatchByAppointmentIdResult.Count > 0) { int opponentId = 0; matchId = selectMatchByAppointmentIdResult[0].MatchId; if (selectMatchByAppointmentIdResult[0].User1 == StoredData.User.UserId) { opponentId = selectMatchByAppointmentIdResult[0].User2.Value; } else { opponentId = selectMatchByAppointmentIdResult[0].User1.Value; } //If a score has already been submitted for the match, then don't delete it. Else delete it. List <SelectMatchPointLogsByMatchIdResult> selectMatchPointLogsByMatchIdResult = dataContext.SelectMatchPointLogsByMatchId(Convert.ToInt32(matchId)).ToList(); if (selectMatchPointLogsByMatchIdResult.Count > 0) { RadAjaxManager.GetCurrent(Page).ResponseScripts.Add(@"radalert('A score has been submitted for this match.<br>This match cannot be deleted.',250,150,'Unable To Delete');"); } else { dataContext.DeleteAppointmentsByMatchId(matchId); //Send email List <SelectUserPublicProfileResult> opponents = dataContext.SelectUserPublicProfile(opponentId).ToList(); StringBuilder message = new StringBuilder(); message.Append("Your opponent has requested the following match to be cancelled\r\n\r\n"); message.Append(string.Format("Players: {0} vs {1}\r\n", StoredData.User.FirstName + " " + StoredData.User.LastName, opponents[0].FirstName + " " + opponents[0].LastName)); message.Append(string.Format("Date & Time: {0} @ {1}\r\n", deletedAppointment.Start.ToShortDateString(), deletedAppointment.Start.ToShortTimeString())); message.Append(string.Format("Your opponent's Phone Number: {0}\r\n", StoredData.User.Phone)); message.Append(string.Format("Your opponent's Email Id: {0}\r\n\r\n", StoredData.User.EmailId)); message.Append("Please respond to this request as soon as you can"); if (!SendEmail(opponents[0].EmailId, message, "Tennis Match cancellation request")) { RadAjaxManager.GetCurrent(Page).ResponseScripts.Add(@"radalert('System was unable to send an email to your opponent about this change<br>Please contact your opponent regarding this update.',250,150,'Error!!');"); } else { RadAjaxManager.GetCurrent(Page).ResponseScripts.Add(@"radalert('Although an email has been sent requesting the cancellation of this match, please contact your opponent to confirm.',250,150,'Match Cancelled!!');"); } } } } } } else { string[] arguments = e.Argument.Split('|'); int gridRowId = Convert.ToInt32(arguments[0]); GridDataItem gridDataItem = playersRadGrid.Items[gridRowId]; int opponentId = Convert.ToInt32(gridDataItem.OwnerTableView.DataKeyValues[gridRowId]["UserId"]); bool opponentAvailabilitySet = false; appointments = GetAppointments(StoredData.User.UserId, opponentId, out opponentAvailabilitySet); if (!opponentAvailabilitySet) { RadAjaxManager.GetCurrent(Page).ResponseScripts.Add(@"radalert('The selected player has not set his/her availability.',250,150,'Availability Not Set!!');"); } } RadScheduler1.DataSource = appointments; RadScheduler1.Rebind(); }