protected void Page_Load(object sender, EventArgs e) { if (Cache["Videos"] != null) { List <ShowVideoControl> collection = Cache["Videos"] as List <ShowVideoControl>; for (int i = 0; i < collection.Count; i++) { pnlVideo.Controls.Add(collection[i]); } pnlVideo.DataBind(); return; } IEnumerable <Video> videosCollection = videos.GetVideos(); List <ShowVideoControl> collectionToSave = new List <ShowVideoControl>(videosCollection.Count() + 1); foreach (Video video in videosCollection) { ShowVideoControl videoControl = LoadControl("ShowVideoControl.ascx") as ShowVideoControl; videoControl.VideoName = video.VideoName; videoControl.VideoLink = video.VideoLink; videoControl.VideoID = video.VideoID; collectionToSave.Add(videoControl); pnlVideo.Controls.Add(videoControl); } pnlVideo.DataBind(); Cache.Insert("Videos", collectionToSave, null, DateTime.Now.AddMinutes(30), Cache.NoSlidingExpiration); }
protected void btnDeleteVideo_Click(object sender, EventArgs e) { if (IsValid == false) { return; } try { int videoID = Convert.ToInt32(txtVideoID.Text); // removing from Cache if (Cache["Videos"] != null) { List <ShowVideoControl> list = Cache["Videos"] as List <ShowVideoControl>; ShowVideoControl videoControl = list.Where(v => v.VideoID == videoID).SingleOrDefault(); list.Remove(videoControl); } string videoName = videos.GetVideo(videoID).VideoName; videos.DeleteVideo(videoID); SetMessageState(State.Success, "הסרטון " + videoName + " נמחק בהצלחה!"); } catch (Exception ex) { SetMessageState(State.Danger, ex.Message); } }
protected void btnAddVideo_Click(object sender, EventArgs e) { // Check if the validation passed successfully if (IsValid == false) { return; } try { // convert from Youtube link to the currect link (https://www.youtube.com/embed/'ID of youtube video') string correctLink = "https://www.youtube.com/embed/" + txtVideoLink.Text.Substring(txtVideoLink.Text.IndexOf("v=") + 2); //Add the video to the Database. videos.AddVideo(txtVideoName.Text, correctLink); //Adding the video to the Cache, checking before if the cache exists! if (Cache["Videos"] != null) { List <ShowVideoControl> list = Cache["Videos"] as List <ShowVideoControl>; ShowVideoControl videoControl = LoadControl("ShowVideoControl.ascx") as ShowVideoControl; // setting VideoID by taking the last VideoID from All // videos in the "list" collection and adding +1 because it a new video videoControl.VideoID = list.Last().VideoID + 1; videoControl.VideoName = txtVideoName.Text; videoControl.VideoLink = correctLink; list.Add(videoControl); } // setting successfully message. SetMessageState(State.Success, "הסרטון " + txtVideoName.Text + " נוסף בהצלחה!"); } catch (Exception ex) { // setting error message. SetMessageState(State.Danger, ex.Message); } }