📄 categoryadmin.cs
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Galleries.Components;
namespace CommunityServer.Galleries.Controls
{
public class CategoryAdmin : GalleryAdminTemplatedWebControl
{
#region Child Controls
private TextBox Name;
private TextBox Description;
private RadioButtonList IsEnabled;
private ParentCategoryDropDown ParentID;
private Literal ErrorLabel;
private Button Save;
private Button Cancel;
#endregion
#region Public Properties
[DefaultValue( "" )]
public virtual string ApplicationKey
{
get
{
Object state = ViewState["ApplicationKey"];
if(state != null)
return (string)state;
return "";
}
set { ViewState["ApplicationKey"] = value; }
}
[DefaultValue( 0 )]
public virtual int CategoryID
{
get
{
Object state = ViewState["CategoryID"];
if(state != null)
return (int)state;
return 0;
}
set { ViewState["CategoryID"] = value; }
}
#endregion
#region Skin
protected override void AttachChildControls()
{
Name = (TextBox)FindControl( "Name" );
Description = (TextBox)FindControl( "Description" );
IsEnabled = (RadioButtonList)FindControl( "IsEnabled" );
ParentID = (ParentCategoryDropDown)FindControl( "ParentID" );
ErrorLabel = (Literal)FindControl( "ErrorLabel" );
Save = (Button)FindControl( "Save" );
Cancel = (Button)FindControl( "Cancel" );
InitializeChildControls();
}
private void InitializeChildControls()
{
Save.Click += new EventHandler(Save_Click);
Cancel.Click += new EventHandler(Cancel_Click);
}
#endregion
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ApplicationKey = CSContext.Current.ApplicationKey;
CategoryID = CSContext.Current.CategoryID;
// Access check
Permissions.AccessCheck( Galleries.GetGallery(ApplicationKey), Permission.Administer, CSContext.Current.User );
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad (e);
if(!Page.IsPostBack)
DataBind();
}
public override void DataBind()
{
base.DataBind();
if(CategoryID != 0)
{
PostCategory category = PostCategories.GetCategory(CategoryID, CategoryType.GalleryPicture, Galleries.GetGalleryID(ApplicationKey), true);
Name.Text = Globals.HtmlDecode(category.Name);
Description.Text = category.Description;
IsEnabled.Items.FindByValue( category.IsEnabled.ToString() ).Selected = true;
ParentID.SectionID = Galleries.GetGalleryID(ApplicationKey);
ParentID.DataBind();
ParentID.SelectedCategoryID = category.ParentID;
}
else
{
Name.Text = "";
Description.Text = "";
IsEnabled.Items.FindByValue( "True" ).Selected = true;
ParentID.SectionID = Galleries.GetGalleryID(ApplicationKey);
ParentID.DataBind();
}
ErrorLabel.Visible = false;
Name.ForeColor = Color.Black;
// Set some resource labels
Save.Text = ResourceManager.GetString("Save");
Cancel.Text = ResourceManager.GetString("Cancel");
}
private void Save_Click(Object sender, EventArgs e)
{
if(CategoryID == 0)
CreateCategory();
else
UpdateCategory();
if(Page.IsValid && !Globals.RedirectSiteUrl())
{
CSContext.Current.Context.Response.Redirect( GalleryUrls.Instance().Admin_ManageCategories(ApplicationKey), true );
}
}
private void CreateCategory()
{
PostCategory category = new PostCategory();
// Do the common stuff
CommonSettings(category);
// Update the database is the page is valid
if(Page.IsValid)
PostCategories.CreateCategory(category);
}
private void UpdateCategory()
{
PostCategory category = PostCategories.GetCategory(CategoryID, CategoryType.GalleryPicture, Galleries.GetGalleryID(ApplicationKey), true);
// Do the common stuff
CommonSettings(category);
// Make sure the parent isn't the same as the current
if(ParentID.SelectedCategoryID != 0)
{
PostCategory pcat = PostCategories.GetCategory(ParentID.SelectedCategoryID, CategoryType.GalleryPicture, ParentID.SectionID, true);
if((pcat.CategoryID == CategoryID) || (pcat.Path.IndexOf("/" + CategoryID + "/") != -1))
((RequiredFieldValidator)FindControl( "ParentIDValidator" )).IsValid = false;
}
// Update the database is the page is valid
if(Page.IsValid)
PostCategories.UpdateCategory(category);
}
private void CommonSettings(PostCategory category)
{
// Basic settings
category.Name = HtmlScrubber.Clean(Name.Text.Trim(), false, true);
category.Description = HtmlScrubber.Clean(Description.Text, false, true);
category.IsEnabled = Boolean.Parse( IsEnabled.SelectedValue );
// Some basic settings we should always have...
category.CategoryType = CategoryType.GalleryPicture;
category.ParentID = ParentID.SelectedCategoryID;
category.SectionID = Galleries.GetGalleryID(ApplicationKey);
// Check to make sure it isn't a duplicate
ArrayList categories = PostCategories.GetCategories(Galleries.GetGalleryID(ApplicationKey), CategoryType.GalleryPicture, true);
foreach(PostCategory cat in categories)
{
if((cat.Name.ToLower() == category.Name.ToLower()) && (cat.CategoryID != category.CategoryID))
{
ErrorLabel.Text = "<span style='color: red'>" + ResourceManager.GetString( "CategoryAdmin_Duplicate" ) + "</span>";
ErrorLabel.Visible = true;
Name.ForeColor = Color.Red;
((RequiredFieldValidator)FindControl( "NameValidator" )).IsValid = false;
}
}
}
private void Cancel_Click(Object sender, EventArgs e)
{
if(!Globals.RedirectSiteUrl())
CSContext.Current.Context.Response.Redirect( GalleryUrls.Instance().Admin_ManageCategories(ApplicationKey), true );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -