protected void BtnCreateEvent_OnClick(object sender, EventArgs e) { DateTime start; DateTime end; if (!ChkBoxDayEvent.Checked) { //Gör om texterna i textboxarna Start- och EndDate till typen DateTime, som används vid skapandet av evenemanget. start = Convert.ToDateTime(TxtBoxStartDate.Text) .Add(TimeSpan.FromHours(Convert.ToDateTime(TxtBoxStartTime.Text).Hour)) .Add(TimeSpan.FromMinutes(Convert.ToDateTime(TxtBoxStartTime.Text).Minute)); end = Convert.ToDateTime(TxtBoxEndDate.Text) .Add(TimeSpan.FromHours(Convert.ToDateTime(TxtBoxEndTime.Text).Hour)) .Add(TimeSpan.FromMinutes(Convert.ToDateTime(TxtBoxEndTime.Text).Minute)); } else { start = Convert.ToDateTime(TxtBoxStartDate.Text); end = Convert.ToDateTime(TxtBoxEndDate.Text).Add(new TimeSpan(23, 59, 59)); } Int32 approxAttend = 0; if (!string.IsNullOrEmpty(TxtBoxApproximateAttendees.Text) && Int32.TryParse(TxtBoxApproximateAttendees.Text, out approxAttend)) { approxAttend = int.Parse(TxtBoxApproximateAttendees.Text); } List<associations> associationsList = new List<associations>(); foreach (ListItem item in ListBoxAssociations.Items) { int aId; if (!String.IsNullOrWhiteSpace(item.Value) && int.TryParse(item.Value, out aId)) { if (AssociationDB.GetAssociationById(aId) != null) { associationsList.Add(AssociationDB.GetAssociationById(aId)); } } } List<subcategories> subCategoriesList = new List<subcategories>(); foreach (ListItem item in ListBoxSubCategories.Items) { int subCatId; if (!String.IsNullOrWhiteSpace(item.Value) && int.TryParse(item.Value, out subCatId)) { if (SubCategoryDB.GetSubCategoryById(subCatId) != null) { subCategoriesList.Add(SubCategoryDB.GetSubCategoryById(subCatId)); } } } //Nytt Event Objekt skapas och alla värdena från formuläret läggs in i objektet var ev = new events { Title = TxtBoxTitle.Text, Description = TxtBoxDescription.Text, Summary = TxtBoxSummary.Text, Other = TxtBoxOther.Text, Location = TxtBoxLocation.Text, ImageUrl = TxtBoxImageUrl.Text, EventUrl = TxtBoxEventUrl.Text, DayEvent = ChkBoxDayEvent.Checked, StartDate = start, EndDate = end, TargetGroup = TxtBoxTargetGroup.Text, ApproximateAttendees = approxAttend , DisplayInCommunity = ChkBoxDisplayInCommunity.Checked, associations = associationsList, subcategories = subCategoriesList, //subcategories = (from ListItem item in ListBoxSubCategories.Items select SubCategoryDB.GetSubCategoryById(int.Parse(item.Value))).ToList(), CreatedBy = HttpContext.Current.User.Identity.Name, UpdatedBy = HttpContext.Current.User.Identity.Name }; //Ger LabelMessage en större font-storlek som visar om eventet kunde skapas eller ej (!!om evenemanget kunde skapas skickas användaren just nu till denna visningssida!!). LabelMessage.Style.Add(HtmlTextWriterStyle.FontSize, "25px"); if (EventDB.AddEvent(ev)) { Response.Redirect( HttpContext.Current.Request.Url.AbsoluteUri.Replace( HttpContext.Current.Request.Url.PathAndQuery, "/") + "EventDetails.aspx?Id=" + ev.Id, false); //LabelMessage.Text = "Event was created"; } else { LabelMessage.ForeColor = Color.Red; LabelMessage.Text = "Event couldn't be created"; } }
protected void BtnUpdateEvent_OnClick(object sender, EventArgs e) { if (CheckUsersPermissionForEvent()) { DateTime start; DateTime end; if (!ChkBoxDayEvent.Checked) { //Gör om texterna i textboxarna Start- och EndDate till typen DateTime, som används vid skapandet av evenemanget. start = Convert.ToDateTime(TxtBoxStartDate.Text) .Add(TimeSpan.FromHours(Convert.ToDateTime(TxtBoxStartTime.Text).Hour)) .Add(TimeSpan.FromMinutes(Convert.ToDateTime(TxtBoxStartTime.Text).Minute)); end = Convert.ToDateTime(TxtBoxEndDate.Text) .Add(TimeSpan.FromHours(Convert.ToDateTime(TxtBoxEndTime.Text).Hour)) .Add(TimeSpan.FromMinutes(Convert.ToDateTime(TxtBoxEndTime.Text).Minute)); } else { start = Convert.ToDateTime(TxtBoxStartDate.Text); end = Convert.ToDateTime(TxtBoxEndDate.Text).Add(new TimeSpan(23, 59, 59)); } Int32 approxAttend = 0; if (!string.IsNullOrEmpty(TxtBoxApproximateAttendees.Text) && Int32.TryParse(TxtBoxApproximateAttendees.Text, out approxAttend)) { approxAttend = int.Parse(TxtBoxApproximateAttendees.Text); } // Hämtar valda Associations i ListBox och lägger dem i en List<associations>. var associationsList = new List<associations>(); foreach (ListItem item in ListBoxAssociations.Items) { int aId; if (!String.IsNullOrWhiteSpace(item.Value) && int.TryParse(item.Value, out aId)) { if (AssociationDB.GetAssociationById(aId) != null) { associationsList.Add(AssociationDB.GetAssociationById(aId)); } } } // Hämtar valda SubCategories i ListBox och lägger dem i en List<subcategories>. var subCategoriesList = new List<subcategories>(); foreach (ListItem item in ListBoxSubCategories.Items) { int subCatId; if (!String.IsNullOrWhiteSpace(item.Value) && int.TryParse(item.Value, out subCatId)) { if (SubCategoryDB.GetSubCategoryById(subCatId) != null) { subCategoriesList.Add(SubCategoryDB.GetSubCategoryById(subCatId)); } } } //Hämtar evenemanget som ska uppdateras. events ev = GetEventToUpdate(); if (ev != null) { int assoId; //Nytt Event Objekt skapas och alla värdena från formuläret läggs in i objektet. var evToUpdate = new events { Id = ev.Id, Title = TxtBoxTitle.Text, Description = TxtBoxDescription.Text, Summary = TxtBoxSummary.Text, Other = TxtBoxOther.Text, Location = TxtBoxLocation.Text, ImageUrl = TxtBoxImageUrl.Text, EventUrl = TxtBoxEventUrl.Text, DayEvent = ChkBoxDayEvent.Checked, StartDate = start, EndDate = end, TargetGroup = TxtBoxTargetGroup.Text, ApproximateAttendees = approxAttend, DisplayInCommunity = ChkBoxDisplayInCommunity.Checked, associations = associationsList, subcategories = subCategoriesList, CreatedBy = HttpContext.Current.User.Identity.Name, UpdatedBy = HttpContext.Current.User.Identity.Name }; //Ger LabelMessage en större font-storlek. LabelMessage.Style.Add(HtmlTextWriterStyle.FontSize, "25px"); if (EventDB.UpdateEvent(evToUpdate) != 0) { //Response.Redirect( // HttpContext.Current.Request.Url.AbsoluteUri.Replace( // HttpContext.Current.Request.Url.PathAndQuery, "/") + "EventDetails.aspx?Id=" + ev.Id, // false); //Server.Transfer(Request.Url.AbsolutePath); LabelMessage.ForeColor = Color.CornflowerBlue; LabelMessage.Text = "Event was updated"; } else { LabelMessage.ForeColor = Color.Red; LabelMessage.Text = "Event couldn't be updated"; } } } else { LabelMessage.ForeColor = Color.Red; LabelMessage.Text = "You have no permission to edit this event!"; } }