⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 managelinksadmin.cs

📁 本系统是在asp版《在线文件管理器》的基础上设计制作
💻 CS
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;

using CommunityServer.Configuration;

namespace CommunityServer.Controls {

    /// <summary>
    /// Allows managment of LinkCategory and Lins
    /// </summary>
	public abstract class ManageLinksAdmin : TemplatedWebControl {

		#region Child Controls

		Repeater LinkCategoriesList;
		Button CreateNewLinkCategory;

		#endregion

        #region Public Properties
      

        #endregion

        #region Abstracts

        protected override String ExternalSkinFileName
        {
            get
            {
                return "Skin-ManageLinksAdmin.ascx";
            }
        }


        /// <summary>
        /// Returns the currect section
        /// </summary>
        protected abstract Section CurrentSection
        {
            get;
        }

        /// <summary>
        /// Property ResourceName (string)
        /// </summary>
        protected abstract string ResourceName
        {
            get;
        }



        #endregion

        #region Skin/Control Events/Methods
		protected override void OnLoad(EventArgs e) 
        {
			if ( !Page.IsPostBack || !EnableViewState ) {
				this.DataBind();
			}
			base.OnLoad( e );
		}


        /// <summary>
        /// Skin WireUp
        /// </summary>
		protected override void AttachChildControls() {

			LinkCategoriesList = FindControl( "LinkCategoriesList" ) as Repeater;
			if ( LinkCategoriesList != null ) {
				LinkCategoriesList.ItemCreated += new RepeaterItemEventHandler(LinkCategoriesList_ItemCreated);
				LinkCategoriesList.ItemDataBound += new RepeaterItemEventHandler(LinkCategoriesList_ItemDataBound);
				LinkCategoriesList.ItemCommand += new RepeaterCommandEventHandler(LinkCategoriesList_ItemCommand);
			}

			CreateNewLinkCategory = FindControl( "CreateNewLinkCategory" ) as Button;
			if ( CreateNewLinkCategory != null ) {
				CreateNewLinkCategory.Click += new EventHandler(CreateNewLinkCategory_Click);
				CreateNewLinkCategory.Text = ResourceManager.GetString( "ManageLinks_CreateNewLinkCategory" );
			}

            ResourceLabel label = FindControl( "LinksTitle" ) as ResourceLabel;
            if(label != null && this.ResourceName !=null)
                label.ResourceName = this.ResourceName;
		}

		public override void DataBind(){
			base.DataBind();

    		LinkCategoriesList.DataSource = LinkCategories.GetCategories( CurrentSection.SectionID, true, false );
			LinkCategoriesList.DataBind();
		}


		private void CleanData() {
			HttpContext context = HttpContext.Current;
			if ( context != null ) {
				context.Response.Redirect( context.Request.RawUrl, true );
			} else {
				DataBind();
			}
		}
        #endregion

		#region LinkCategoriesList Event Handlers

		private void LinkCategoriesList_ItemCreated( Object sender, RepeaterItemEventArgs e ) 
        {
			switch ( e.Item.ItemType ) {
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
				case ListItemType.SelectedItem:

					Repeater LinksList = e.Item.FindControl( "LinksList" ) as Repeater;
					if ( LinksList != null ) {
						LinksList.DataBinding += new EventHandler(LinksList_DataBinding);
						LinksList.ItemDataBound += new RepeaterItemEventHandler(LinksList_ItemDataBound);
						LinksList.ItemCommand += new RepeaterCommandEventHandler(LinksList_ItemCommand);
					}

					Button CreateNewLink = e.Item.FindControl( "CreateNewLink" ) as Button;
					if ( CreateNewLink != null ) {
						CreateNewLink.Click += new EventHandler(CreateNewLink_Click);
						CreateNewLink.Text = ResourceManager.GetString( "ManageLinks_CreateNewLink" );
					}

					break;
			}
		}

		private void LinkCategoriesList_ItemDataBound(object sender, RepeaterItemEventArgs e) {
			switch ( e.Item.ItemType ) {
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
				case ListItemType.SelectedItem:

					LinkCategory blogLinkCategory = (LinkCategory)e.Item.DataItem;


					TextBox LinkCategoryName = e.Item.FindControl( "LinkCategoryName" ) as TextBox;
					if ( LinkCategoryName != null ) {
						LinkCategoryName.Text = blogLinkCategory.Name;
					}

					TextBox LinkCategoryDescription = e.Item.FindControl( "LinkCategoryDescription" ) as TextBox;
					if ( LinkCategoryDescription != null ) {
						LinkCategoryDescription.Text = blogLinkCategory.Description;
					}

					CheckBox LinkCategoryIsEnabled = e.Item.FindControl( "LinkCategoryIsEnabled" ) as CheckBox;
					if ( LinkCategoryIsEnabled != null ) {
						LinkCategoryIsEnabled.Checked = blogLinkCategory.IsEnabled;
					}

					Button saveLinkCategory = e.Item.FindControl( "SaveLinkCategory" ) as Button;
					if ( saveLinkCategory != null ) {
						saveLinkCategory.Text = ResourceManager.GetString( "Save" );
						saveLinkCategory.CommandName = "SaveLinkCategory";
						saveLinkCategory.CommandArgument = blogLinkCategory.LinkCategoryID.ToString();
					}

					Button deleteLinkCategory = e.Item.FindControl( "DeleteLinkCategory" ) as Button;
					if ( deleteLinkCategory != null ) {
						deleteLinkCategory.Text = ResourceManager.GetString( "Delete" );
						deleteLinkCategory.CommandName = "DeleteLinkCategory";
                        deleteLinkCategory.Attributes.Add("onclick",string.Format("return confirm('{0}');",ResourceManager.GetString("ManageLinks_ConfirmDeleteCategory")));
						deleteLinkCategory.CommandArgument = blogLinkCategory.LinkCategoryID.ToString();
					}

					ThemedImageButton moveUp = e.Item.FindControl( "MoveCategoryUp" ) as ThemedImageButton;
					if ( moveUp != null ) {
						moveUp.CommandName = "MoveUp";
						moveUp.CommandArgument = blogLinkCategory.LinkCategoryID.ToString();
					}

					ThemedImageButton moveDown = e.Item.FindControl( "MoveCategoryDown" ) as ThemedImageButton;
					if ( moveDown != null ) {
						moveDown.CommandName = "MoveDown";
						moveDown.CommandArgument = blogLinkCategory.LinkCategoryID.ToString();
					}

					Button CreateNewLink = e.Item.FindControl( "CreateNewLink" ) as Button;
					if ( CreateNewLink != null ) {
						CreateNewLink.CommandArgument = blogLinkCategory.LinkCategoryID.ToString();
					}

					break;
			}
		}

		#endregion

		#region LinksList Event Handlers

		private void LinksList_DataBinding(object sender, EventArgs e) {
			Repeater LinksList = (Repeater)sender;

			RepeaterItem container = (RepeaterItem)LinksList.NamingContainer;
			LinkCategory category = (LinkCategory)container.DataItem;

			LinksList.DataSource = Links.GetLinks( category.LinkCategoryID, true, false );
		}

		private void LinksList_ItemDataBound(object sender, RepeaterItemEventArgs e) {
			switch ( e.Item.ItemType ) {
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
				case ListItemType.SelectedItem:

					Link blogLink = (Link)e.Item.DataItem;
					String commandArg = blogLink.LinkID.ToString() + ":" + blogLink.LinkCategoryID.ToString();

					TextBox LinkTitle = e.Item.FindControl( "LinkTitle" ) as TextBox;
					if ( LinkTitle != null ) {
						LinkTitle.Text = blogLink.Title;
					}

					TextBox LinkUrl = e.Item.FindControl( "LinkUrl" ) as TextBox;
					if ( LinkUrl != null ) {
						LinkUrl.Text = blogLink.Url;
					}

					CheckBox LinkIsEnabled = e.Item.FindControl( "LinkIsEnabled" ) as CheckBox;
					if ( LinkIsEnabled != null ) {
						LinkIsEnabled.Checked = blogLink.IsEnabled;
					}

					Button saveLink = e.Item.FindControl( "SaveLink" ) as Button;
					if ( saveLink != null ) {
						saveLink.Text = ResourceManager.GetString( "Save" );
						saveLink.CommandName = "SaveLink";
						saveLink.CommandArgument = commandArg;
					}

					Button deleteLink = e.Item.FindControl( "DeleteLink" ) as Button;
					if ( deleteLink != null ) {
						deleteLink.Text = ResourceManager.GetString( "Delete" );
                        deleteLink.Attributes.Add("onclick",string.Format("return confirm('{0}');",ResourceManager.GetString("ManageLinks_ConfirmDeleteLink")));
						deleteLink.CommandName = "DeleteLink";
						deleteLink.CommandArgument = commandArg;
					}

					ThemedImageButton moveUp = e.Item.FindControl( "MoveLinkUp" ) as ThemedImageButton;
					if ( moveUp != null ) {
						moveUp.CommandName = "MoveUp";
						moveUp.CommandArgument = commandArg;
					}

					ThemedImageButton moveDown = e.Item.FindControl( "MoveLinkDown" ) as ThemedImageButton;
					if ( moveDown != null ) {
						moveDown.CommandName = "MoveDown";
						moveDown.CommandArgument = commandArg;
					}

					break;
			}
		}

		#endregion

		#region Button Click Handlers

		private void LinkCategoriesList_ItemCommand(object source, RepeaterCommandEventArgs e) {
			Int32 linkCategoryID = Int32.Parse( e.CommandArgument.ToString() );
			
			switch( e.CommandName ) {
				case "MoveUp":
					MoveCategoryUp( linkCategoryID );
					break;
				case "MoveDown":
					MoveCategoryDown( linkCategoryID );
					break;
				case "SaveLinkCategory":
					SaveLinkCategory( linkCategoryID, ((Control)e.CommandSource).NamingContainer );
					break;
				case "DeleteLinkCategory":
					DeleteLinkCategory( linkCategoryID );
					break;
			}

			CleanData();
		}

		private void LinksList_ItemCommand(object source, RepeaterCommandEventArgs e) {
			String[] args = e.CommandArgument.ToString().Split( ':' );
			if ( args.Length != 2 ) {
				return;
			}

			Int32 linkID = Int32.Parse( args[ 0 ] );
			Int32 linkCategoryID = Int32.Parse( args[ 1 ] );

			switch ( e.CommandName ) {
				case "MoveUp":
					MoveLinkUp( linkID, linkCategoryID );
					break;
				case "MoveDown":
					MoveLinkDown( linkID, linkCategoryID );
					break;
				case "SaveLink":
					SaveLink( linkID, linkCategoryID, ((Control)e.CommandSource).NamingContainer );
					break;
				case "DeleteLink":
					DeleteLink( linkID, linkCategoryID );
					break;
			}

			CleanData();
		}

		private void CreateNewLinkCategory_Click(object sender, EventArgs e) {
			CreateLinkCategory( ((Control)sender).NamingContainer );
			CleanData();
		}

		private void CreateNewLink_Click(object sender, EventArgs e) {
			Button newLinkButton = sender as Button;
			CreateLink( Int32.Parse( newLinkButton.CommandArgument.ToString() ), newLinkButton.NamingContainer );
			CleanData();
		}

		#endregion

		#region Command Implementors

		private void MoveLinkUp( Int32 linkID, Int32 linkCategoryID ) {
			Links.ChangeLinkSortOrder( linkID, linkCategoryID, true );
		}

		private void MoveLinkDown( Int32 linkID, Int32 linkCategoryID ) {
			Links.ChangeLinkSortOrder( linkID, linkCategoryID, false );
		}

		private void MoveCategoryUp( Int32 linkCategoryID ) {
			LinkCategories.ChangeCategorySortOrder( linkCategoryID, CurrentSection.SectionID, true );
		}

		private void MoveCategoryDown( Int32 linkCategoryID ) {
			LinkCategories.ChangeCategorySortOrder( linkCategoryID, CurrentSection.SectionID, false );
            
		}


		private void CreateLinkCategory( Control namingContainer ) {
			TextBox NewLinkCategoryName = namingContainer.FindControl( "NewLinkCategoryName" ) as TextBox;
			TextBox NewLinkCategoryDescription = namingContainer.FindControl( "NewLinkCategoryDescription" ) as TextBox;

            if(NewLinkCategoryName.Text.Trim().Length == 0)
                return;

			LinkCategory newCategory = new LinkCategory();
			newCategory.Name = NewLinkCategoryName.Text;
			newCategory.Description = NewLinkCategoryDescription.Text;
			newCategory.ForumID = CurrentSection.SectionID;
			newCategory.IsEnabled = true;
			newCategory.SortOrder = 0;

			LinkCategories.CreateCategory( newCategory );
		}
		private void SaveLinkCategory( Int32 linkCategoryID, Control namingContainer ) {

			TextBox LinkCategoryName = namingContainer.FindControl( "LinkCategoryName" ) as TextBox;
			TextBox LinkCategoryDescription = namingContainer.FindControl( "LinkCategoryDescription" ) as TextBox;
			CheckBox LinkCategoryIsEnabled = namingContainer.FindControl( "LinkCategoryIsEnabled" ) as CheckBox;

            if(LinkCategoryName.Text.Trim().Length == 0)
                return;

            LinkCategory category = LinkCategories.GetCategory( linkCategoryID, CurrentSection.SectionID, true );
			
			category.Name = LinkCategoryName.Text;
			category.Description = LinkCategoryDescription.Text;
			category.IsEnabled = LinkCategoryIsEnabled.Checked;

			LinkCategories.UpdateCategory( category );
			
		}

		private void DeleteLinkCategory( Int32 linkCategoryID ) {
			LinkCategories.DeleteCategory( linkCategoryID, CurrentSection.SectionID );
		}

		
        private bool ValidateLink(TextBox title, TextBox url)
        {
            return title.Text.Trim().Length > 0 && url.Text.Trim().Length > 0;
        }

		private void CreateLink( Int32 linkCategoryID, Control namingContainer ) {
			TextBox NewLinkTitle = namingContainer.FindControl( "NewLinkTitle" ) as TextBox;
			TextBox NewLinkUrl = namingContainer.FindControl( "NewLinkUrl" ) as TextBox;

            if(!ValidateLink(NewLinkTitle,NewLinkUrl))
                return;
            

			Link newLink = new Link();
			newLink.Title = NewLinkTitle.Text;
			newLink.Url = NewLinkUrl.Text;
			newLink.LinkCategoryID = linkCategoryID;
			newLink.IsEnabled = true;
			newLink.SortOrder = 0;

			Links.CreateLink( newLink );
		}
		private void SaveLink( Int32 linkID, Int32 linkCategoryID, Control namingContainer ) {
			
			TextBox LinkTitle = namingContainer.FindControl( "LinkTitle" ) as TextBox;
			TextBox LinkUrl = namingContainer.FindControl( "LinkUrl" ) as TextBox;
			CheckBox LinkIsEnabled = namingContainer.FindControl( "LinkIsEnabled" ) as CheckBox;

            if(!ValidateLink(LinkTitle,LinkUrl))
                return;

            Link link = Links.GetLink( linkID, linkCategoryID, true );

			link.Title = LinkTitle.Text;
			link.Url = LinkUrl.Text;
			link.IsEnabled = LinkIsEnabled.Checked;
			
			Links.UpdateLink( link );
		}

		private void DeleteLink( Int32 linkID, Int32 linkCategoryID ) {
			Links.DeleteLink( linkID, linkCategoryID );
		}


		#endregion

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -