// The id parameter name should match the DataKeyNames value set on the control public void FormViewPlaylist_UpdateItem(int id) { var item = data.Playlists.GetById(id); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", id)); return; } if (item.CreatorId != Context.User.Identity.GetUserId()) { master.ShowErrorMessage("NOT AUTHORIZED! Not your playlist."); return; } TryUpdateModel(item); if (ModelState.IsValid) { item.Videos.Clear(); var lbVideos = (ListBox)FormViewPlaylist.FindControl("ListBoxEditVideos"); foreach (ListItem ev in lbVideos.Items) { var vidId = int.Parse(ev.Value); var videoToAdd = data.Videos.GetById(vidId); item.Videos.Add(videoToAdd); } data.SaveChanges(); master.ShowSuccessMessage("Updated!"); } }
protected void RemoveVideo_Click(object sender, EventArgs e) { var lb = (ListBox)FormViewPlaylist.FindControl("ListBoxEditVideos"); var itemsToRemove = new List <ListItem>(); foreach (ListItem item in lb.Items) { if (item.Selected) { itemsToRemove.Add(item); } } foreach (var item in itemsToRemove) { lb.Items.Remove(item); } }
public void FormViewPlaylist_InsertItem() { var playlist = new YouTubePlaylists.Models.Playlist(); TryUpdateModel(playlist); playlist.CreatorId = Context.User.Identity.GetUserId(); var tbVideoUrls = (TextBox)FormViewPlaylist.FindControl("tbVideoUrls"); if (tbVideoUrls != null) { var videoUrls = tbVideoUrls.Text.Split(' '); foreach (var url in videoUrls) { var video = new Video { Url = url }; playlist.Videos.Add(video); } } try { data.Playlists.Add(playlist); data.SaveChanges(); master.ShowSuccessMessage("Playlist added."); Response.Redirect("/Playlists/Playlist.aspx?id=" + playlist.Id); } catch (Exception e) { master.ShowErrorMessage("Playlist not added. Invalid data."); } }