PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/EntityFramework/Beta3/MyTravel_WCF_Sample/ObjectServices_MyTravel_CS/MyTravelPostWebsite/TravelPage.aspx.cs

#
C# | 246 lines | 148 code | 43 blank | 55 comment | 4 complexity | 77f93af26d5aec0077eae80c79dc6f66 MD5 | raw file
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.Security;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Web.UI.HtmlControls;
  13. using System.Xml.Linq;
  14. using MyTravelPostWebsite.MyTravelPostService;
  15. using System.Collections.Generic;
  16. namespace MyTravelPostWebsite
  17. {
  18. public partial class TravelPage : System.Web.UI.Page
  19. {
  20. protected void Page_Load(object sender, EventArgs e)
  21. {
  22. // check the post back
  23. if (IsPostBack)
  24. {
  25. return;
  26. }
  27. // get the blog id from the page request.
  28. int blogID = 0;
  29. int.TryParse(Request.Params["BlogID"], out blogID);
  30. // create a short lived service and request for latest blog post
  31. using (MyTravelPostServiceClient travelEntitiesService = new MyTravelPostServiceClient())
  32. {
  33. Blog blog = travelEntitiesService.GetBlog(blogID);
  34. BlogPost post = travelEntitiesService.GetLatestBlogPosting(blog);
  35. if (null != post)
  36. {
  37. // fill in the new text.
  38. labelBlogTitle.Text = post.BlogTitle;
  39. labelSeries.Text = blog.SeriesTitle;
  40. labelPerson.Text = blog.BlogWriter.Name;
  41. blogEntry.Text = post.BlogEntry;
  42. // call the methods to update the comments.
  43. UpdateCommentsList(post);
  44. var people = travelEntitiesService.GetPeople();
  45. // not set all the people in the dropdown list
  46. dropdownPersons.DataTextField = "Name";
  47. dropdownPersons.DataSource = people;
  48. dropdownPersons.DataBind();
  49. dropdownBlogPersonChange.DataTextField = "Name";
  50. dropdownBlogPersonChange.DataSource = people;
  51. dropdownBlogPersonChange.DataBind();
  52. // setup the viewstate that we need in the submit button
  53. ViewState["BlogPost"] = post;
  54. ViewState["People"] = people;
  55. ViewState["Blog"] = blog;
  56. }
  57. }
  58. }
  59. // on submit, we need to add a comment that's associated to the blog post.
  60. protected void buttonSubmitComment_Click(object sender, EventArgs e)
  61. {
  62. // create a short lived service and send back the travel blogs.
  63. using (MyTravelPostServiceClient travelEntitiesService = new MyTravelPostServiceClient())
  64. {
  65. BlogPost post = (BlogPost)ViewState["BlogPost"];
  66. List<Person> peopleList = (List<Person>)ViewState["People"];
  67. // get the person from the people list
  68. Person person = peopleList[dropdownPersons.SelectedIndex];
  69. Comment newComment = new Comment();
  70. newComment.CommentText = textComment.Text;
  71. // what we are doing here is setting up the relationships, similar
  72. // to a foriegn key. If you didn't have the EntityKey on the reference
  73. // then you would have to send all the related ends.
  74. // this case we can optimize and build the relationships
  75. // and send them down to be added in the ObjectContext.
  76. newComment.PersonReference = new EntityReferenceOfPersonXC1u02lO();
  77. newComment.PersonReference.EntityKey = person.EntityKey;
  78. newComment.BlogPostReference = new EntityReferenceOfBlogPostXC1u02lO();
  79. newComment.BlogPostReference.EntityKey = post.EntityKey;
  80. // call the service that adds the comment to the database.
  81. newComment = travelEntitiesService.AddComment(newComment);
  82. newComment.CommentWriter = person;
  83. // add to the overall list of comments, and update the post.
  84. post.PostComments.Add(newComment);
  85. // clear the text.
  86. textComment.Text = "";
  87. // call the methods to update the comments on the field
  88. UpdateCommentsList(post);
  89. // reload page so that F5, refresh doesn't update all this data.
  90. ReloadPage();
  91. }
  92. }
  93. private void ReloadPage()
  94. {
  95. // get the blog and reload this page so that refresh won't give us
  96. // bad data.
  97. Blog blog = (Blog)ViewState["Blog"];
  98. // launch this page again.
  99. Response.Redirect("TravelPage.aspx?BlogID=" + blog.BlogID);
  100. }
  101. private void UpdateCommentsList(BlogPost post)
  102. {
  103. // get all the comments with people and load the comments grid.
  104. var comments = from c in post.PostComments
  105. select new
  106. {
  107. Author = c.CommentWriter.Name,
  108. WhatISaid = c.CommentText,
  109. };
  110. // reset the blog post into comments.
  111. ViewState["BlogPost"] = post;
  112. gridComments.DataSource = comments;
  113. gridComments.DataBind();
  114. }
  115. protected void gridComments_RowDeleted(object sender, GridViewDeletedEventArgs e)
  116. {
  117. // we did all the work in RowDeleting
  118. }
  119. protected void gridComments_RowDeleting(object sender, GridViewDeleteEventArgs e)
  120. {
  121. // create a short lived service and send back the travel blogs.
  122. using (MyTravelPostServiceClient travelEntitiesService = new MyTravelPostServiceClient())
  123. {
  124. // get the comment that is being marked for deletion
  125. // and get the view state blog post.
  126. BlogPost post = (BlogPost)ViewState["BlogPost"];
  127. // move the comment to the deleted comment selection.
  128. Comment deletedComment = post.PostComments[e.RowIndex];
  129. // remove the post comment from the deleted item.
  130. post.PostComments.RemoveAt(e.RowIndex);
  131. // serialize the post and the deleted comments back to view state.
  132. ViewState["BlogPost"] = post;
  133. // call the DeleteComment service
  134. travelEntitiesService.DeleteComment(deletedComment);
  135. // update the comments
  136. UpdateCommentsList(post);
  137. // reload page so that F5, refresh doesn't update all this data.
  138. ReloadPage();
  139. }
  140. }
  141. protected void buttonUpdate_Click(object sender, EventArgs e)
  142. {
  143. // create the update service
  144. using (MyTravelPostServiceClient travelEntitiesService = new MyTravelPostServiceClient())
  145. {
  146. // here we are going to take every other comment and put (*) at the end.
  147. // this is just to show how to make the web service call and update
  148. // items on the other end.
  149. BlogPost post = (BlogPost)ViewState["BlogPost"];
  150. List<Comment> updatedComments = new List<Comment>();
  151. // this bool value is so we can pass in a subset of items to updated.
  152. bool toupdate = true;
  153. foreach (Comment c in post.PostComments)
  154. {
  155. if (toupdate)
  156. {
  157. // create a new comment and just set the id and entity key
  158. // that's all we need to update the new comment text.
  159. Comment newComment = new Comment();
  160. newComment.EntityKey = c.EntityKey;
  161. newComment.CommentID = c.CommentID;
  162. newComment.CommentText = c.CommentText + "*";
  163. updatedComments.Add(newComment);
  164. toupdate = false;
  165. }
  166. else
  167. {
  168. toupdate = true;
  169. }
  170. }
  171. // even through the comments are also attached to the post
  172. // we can drop and just send the updated items. This helps in
  173. // reducing the amount of data going through the service.
  174. post.PostComments
  175. = travelEntitiesService.UpdateComments(post.PostComments, updatedComments);
  176. // update the view state, and be sure to update the gridview
  177. ViewState["BlogPost"] = post;
  178. UpdateCommentsList(post);
  179. // reload page so that F5, refresh doesn't update all this data.
  180. ReloadPage();
  181. }
  182. }
  183. protected void ChangeOwner_Click(object sender, EventArgs e)
  184. {
  185. // create the update service
  186. using (MyTravelPostServiceClient travelEntitiesService = new MyTravelPostServiceClient())
  187. {
  188. // here we are going to take every other comment and put (*) at the end.
  189. // this is just to show how to make the web service call and update
  190. // items on the other end.
  191. Blog blog = (Blog)ViewState["Blog"];
  192. // get the person from the people list
  193. List<Person> peopleList = (List<Person>)ViewState["People"];
  194. Person person = peopleList[dropdownBlogPersonChange.SelectedIndex];
  195. EntityKey personKey = person.EntityKey;
  196. // use the key from the person to change the ownship of this blog
  197. travelEntitiesService.ChangeBlogowner(blog, personKey);
  198. // reload page so that F5, refresh doesn't update all this data.
  199. ReloadPage();
  200. }
  201. }
  202. }
  203. }