PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/admin/Posts/Categories.aspx.cs

#
C# | 193 lines | 135 code | 23 blank | 35 comment | 21 complexity | 4955dd280bdcfe178b6fb2de779a6335 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. #region Using
  2. using System;
  3. using System.Web.UI.WebControls;
  4. using BlogEngine.Core;
  5. using App_Code;
  6. #endregion
  7. namespace Admin.Posts
  8. {
  9. public partial class Categories : System.Web.UI.Page
  10. {
  11. /// <summary>
  12. /// Handles the Load event of the Page control.
  13. /// </summary>
  14. /// <param name="sender">The source of the event.</param>
  15. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. WebUtils.CheckRightsForAdminPostPages(false);
  19. if (!Page.IsPostBack)
  20. {
  21. BindGrid();
  22. LoadParentDropDown(ddlNewParent, null);
  23. }
  24. grid.RowEditing += new GridViewEditEventHandler(grid_RowEditing);
  25. grid.RowUpdating += new GridViewUpdateEventHandler(grid_RowUpdating);
  26. grid.RowCancelingEdit += delegate { Response.Redirect(Request.RawUrl); };
  27. grid.RowDeleting += new GridViewDeleteEventHandler(grid_RowDeleting);
  28. grid.RowDataBound += new GridViewRowEventHandler(grid_RowDataBound);
  29. btnAdd.Click += new EventHandler(btnAdd_Click);
  30. btnAdd.Text = Resources.labels.add + " " + Resources.labels.category.ToLowerInvariant();
  31. valExist.ServerValidate += new ServerValidateEventHandler(valExist_ServerValidate);
  32. Page.Title = Resources.labels.categories;
  33. }
  34. void grid_RowDataBound(object sender, GridViewRowEventArgs e)
  35. {
  36. if (e.Row.RowState == DataControlRowState.Edit ||
  37. e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
  38. {
  39. Category self = (Category)e.Row.DataItem;
  40. DropDownList ddlParent = (DropDownList)e.Row.FindControl("ddlParent");
  41. LoadParentDropDown(ddlParent, self);
  42. Category temp = (Category)e.Row.DataItem;
  43. if (temp.Parent != null)
  44. {
  45. foreach (ListItem item in ddlParent.Items)
  46. {
  47. if (item.Value == temp.Parent.ToString())
  48. {
  49. item.Selected = true;
  50. break;
  51. }
  52. }
  53. }
  54. }
  55. }
  56. private void LoadParentDropDown(DropDownList ddl, Category self)
  57. {
  58. // Load up the Parent DropDown
  59. ddl.ClearSelection();
  60. ddl.Items.Add(new ListItem("none", "0"));
  61. foreach (Category cat in Category.Categories)
  62. {
  63. if (self == null || !cat.Id.Equals(self.Id))
  64. ddl.Items.Add(new ListItem(cat.CompleteTitle(), cat.Id.ToString()));
  65. }
  66. }
  67. /// <summary>
  68. /// Handles the ServerValidate event of the valExist control.
  69. /// </summary>
  70. /// <param name="source">The source of the event.</param>
  71. /// <param name="args">The <see cref="System.Web.UI.WebControls.ServerValidateEventArgs"/> instance containing the event data.</param>
  72. private void valExist_ServerValidate(object source, ServerValidateEventArgs args)
  73. {
  74. args.IsValid = true;
  75. foreach (Category category in Category.Categories)
  76. {
  77. if (category.Title.Equals(txtNewCategory.Text.Trim(), StringComparison.OrdinalIgnoreCase))
  78. args.IsValid = false;
  79. }
  80. }
  81. /// <summary>
  82. /// Handles the Click event of the btnAdd control.
  83. /// </summary>
  84. /// <param name="sender">The source of the event.</param>
  85. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  86. void btnAdd_Click(object sender, EventArgs e)
  87. {
  88. if (Page.IsValid)
  89. {
  90. string description = txtNewNewDescription.Text;
  91. if (description.Length > 255)
  92. description = description.Substring(0, 255);
  93. Category cat = new Category(txtNewCategory.Text, description);
  94. if (ddlNewParent.SelectedValue != "0")
  95. cat.Parent = new Guid(ddlNewParent.SelectedValue);
  96. cat.Save();
  97. Response.Redirect(Request.RawUrl, true);
  98. }
  99. }
  100. /// <summary>
  101. /// Handles the RowDeleting event of the grid control.
  102. /// </summary>
  103. /// <param name="sender">The source of the event.</param>
  104. /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewDeleteEventArgs"/> instance containing the event data.</param>
  105. void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
  106. {
  107. Guid id = (Guid)grid.DataKeys[e.RowIndex].Value;
  108. Category cat = Category.GetCategory(id);
  109. // Removes all references to the category
  110. foreach (Post post in Post.Posts)
  111. {
  112. if (post.Categories.Contains(cat))
  113. {
  114. post.Categories.Remove(cat);
  115. }
  116. }
  117. cat.Delete();
  118. cat.Save();
  119. Response.Redirect(Request.RawUrl);
  120. }
  121. /// <summary>
  122. /// Handles the RowUpdating event of the grid control.
  123. /// </summary>
  124. /// <param name="sender">The source of the event.</param>
  125. /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewUpdateEventArgs"/> instance containing the event data.</param>
  126. void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
  127. {
  128. Guid id = (Guid)grid.DataKeys[e.RowIndex].Value;
  129. TextBox textboxTitle = (TextBox)grid.Rows[e.RowIndex].FindControl("txtTitle");
  130. TextBox textboxDescription = (TextBox)grid.Rows[e.RowIndex].FindControl("txtDescription");
  131. DropDownList ddlParent = (DropDownList)grid.Rows[e.RowIndex].FindControl("ddlParent");
  132. Category cat = Category.GetCategory(id);
  133. cat.Title = textboxTitle.Text;
  134. cat.Description = textboxDescription.Text;
  135. if (ddlParent.SelectedValue == "0")
  136. cat.Parent = null;
  137. else
  138. cat.Parent = new Guid(ddlParent.SelectedValue);
  139. cat.Save();
  140. Response.Redirect(Request.RawUrl);
  141. }
  142. /// <summary>
  143. /// Handles the RowEditing event of the grid control.
  144. /// </summary>
  145. /// <param name="sender">The source of the event.</param>
  146. /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewEditEventArgs"/> instance containing the event data.</param>
  147. void grid_RowEditing(object sender, GridViewEditEventArgs e)
  148. {
  149. grid.EditIndex = e.NewEditIndex;
  150. BindGrid();
  151. }
  152. /// <summary>
  153. /// Binds the grid with all the categories.
  154. /// </summary>
  155. private void BindGrid()
  156. {
  157. grid.DataKeyNames = new string[] { "Id" };
  158. grid.DataSource = Category.Categories;
  159. grid.DataBind();
  160. }
  161. protected string GetParentTitle(object item)
  162. {
  163. Category temp = (Category)item;
  164. if (temp.Parent == null)
  165. return "";
  166. else
  167. return Category.GetCategory((Guid)temp.Parent).Title;
  168. }
  169. }
  170. }