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