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

📄 searchresults.cs

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

using System;
using System.Collections;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Summary description for SearchResults.
	/// </summary>
	public class SearchResults : GalleryTemplatedWebControl
	{

		#region Child Controls

		private string[] searchSections;
		private string[] searchUsers;

		private Label searchFor;
		private TextBox searchTextTop;
		private TextBox searchTextBottom;
		private LinkButton searchButtonTop;
		private LinkButton searchButtonBottom;
		private DataList searchDataList;
		private CurrentPage resultTotal;
		private Literal searchDuration;
		private Pager pager;
		private HtmlGenericControl validatorTop;
		private HtmlGenericControl validatorBottom;

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			searchFor				= (Label)FindControl("ForumDescription");
			searchTextTop           = (TextBox)FindControl("SearchTextTop");
			searchTextBottom        = (TextBox)FindControl("SearchTextBottom");
			searchButtonTop         = (LinkButton)FindControl("SearchButtonTop");
			searchButtonBottom      = (LinkButton)FindControl("SearchButtonBottom");
			searchDataList          = (DataList)FindControl("SearchDataList");
			pager                   = (Pager)FindControl("Pager");
			resultTotal             = (CurrentPage)FindControl("TotalResults");
			searchDuration          = (Literal)FindControl("SearchDuration");
			validatorTop			= (HtmlGenericControl)FindControl("SearchBoxValidatorTop");
			validatorBottom			= (HtmlGenericControl)FindControl("SearchBoxValidatorBottom");

			if(searchTextTop == null)
				throw new Exception("!danm!");

			InitializeChildControls();
		}

		private void InitializeChildControls()
		{
			searchButtonTop.Text    = ResourceManager.GetString("Search");
			searchButtonBottom.Text = ResourceManager.GetString("Search");
			resultTotal.TextFormat  = ResourceManager.GetString("SearchResults_TotalResults");
			searchDuration.Text     = ResourceManager.GetString("SearchResults_SearchDuration");

			((Label)FindControl("ForumName")).Text = ResourceManager.GetString("Gallery_Search_Title");
			pager.PageSize = CSContext.Current.SiteSettings.SearchPostsPerPage;

			searchDataList.ItemDataBound += new DataListItemEventHandler(searchDataList_ItemDataBound);
			searchButtonTop.Click += new EventHandler(Search_Click);
			searchButtonBottom.Click += new EventHandler(Search_Click);
		}

		#endregion

		#region DataBind

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			if(Page.Request.QueryString["f"] != null)
				searchSections = SectionsToSearchDecode(Page.Request.QueryString["f"]);
		}


		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			DataBind();
		}

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

			if(!Page.IsPostBack) 
			{
				searchTextTop.Text = Page.Request.QueryString["q"];
				searchTextBottom.Text = searchTextTop.Text;
				searchUsers = Search.UsersToDecode(Page.Request.QueryString["u"] );
			}
			
			BindSearch();
		}

		public void BindSearch() 
		{
			SearchResultSet searchResult;

			// Set the "search for" text
			searchFor.Text = string.Format(ResourceManager.GetString("SearchViewSimple_SearchFor"), Globals.HtmlEncode(searchTextTop.Text));

			// Get the search results
			SearchQuery query = new SearchQuery();
			query.PageIndex = pager.PageIndex;
			query.PageSize = pager.PageSize;
			query.UserID = CSContext.Current.User.UserID;
			query.SectionsToSearch = searchSections;
			query.UsersToSearch = searchUsers;
			query.SearchTerms = searchTextTop.Text;
			GallerySearch search = new GallerySearch();
			searchResult = search.GetSearchResults(query);

			// Do we have search results?
			if(!searchResult.HasResults) 
				return;

			// Set the TotalRecords in the pager
			pager.TotalRecords = searchResult.TotalRecords;

			// Databind
			searchDataList.DataSource = searchResult.Posts;
			searchDataList.DataBind();

			// Setup the result total
			resultTotal.PageIndex = pager.PageIndex;
			resultTotal.TotalPages = pager.CalculateTotalPages();
			resultTotal.TotalRecords = pager.TotalRecords;
			pager.DataBind();

			if (searchResult.SearchDuration < 1)
				searchDuration.Text = string.Format(searchDuration.Text, "< 1");
			else
				searchDuration.Text = string.Format(searchDuration.Text, searchResult.SearchDuration);
		}

		#endregion

		#region ItemDataBound

		private void searchDataList_ItemDataBound(object sender, DataListItemEventArgs e)
		{
            Picture picture = e.Item.DataItem as Picture;

			switch(e.Item.ItemType)
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:

					HyperLink pictureLink = (HyperLink) e.Item.FindControl("PictureLink");
					HyperLink galleryLink = (HyperLink) e.Item.FindControl("GalleryLink");
					HyperLink userLink = (HyperLink) e.Item.FindControl("UserLink");
					Literal postDate = (Literal) e.Item.FindControl("PostDate");

					Gallery gallery = Galleries.GetGallery( picture.SectionID );

					pictureLink.Text = picture.Subject;
					pictureLink.NavigateUrl = GalleryUrls.Instance().ViewPicture( gallery.ApplicationKey, picture );

					galleryLink.Text = gallery.Name;
					galleryLink.NavigateUrl = GalleryUrls.Instance().ViewGallery( gallery.ApplicationKey );

					userLink.Text = picture.Username;
					userLink.NavigateUrl = Globals.GetSiteUrls().UserProfile( picture.AuthorID );
					userLink.Target = "_blank";

					User user = CSContext.Current.User;
					DateTime changedDate = picture.PostDate;
					string dateFormat;

					if(!user.IsAnonymous)
					{
						changedDate = user.GetTimezone(changedDate);
						dateFormat = user.Profile.DateFormat;
					}
					else
						dateFormat = CSContext.Current.SiteSettings.DateFormat;
						
					postDate.Text = changedDate.ToString(dateFormat) + " " + changedDate.ToString(CSContext.Current.SiteSettings.TimeFormat);

					break;
			}
		}

		#endregion

		#region Event Handlers

		public void Search_Click (Object sender, EventArgs e) 
		{
			bool isSearchTextInTopTextBox = true;

			// Which search text is being used?
			if( ((LinkButton) sender).ID != "SearchButtonTop" )
				isSearchTextInTopTextBox = false;

			// Set the text to the correct values
			if(isSearchTextInTopTextBox)
			{
				if(searchTextTop.Text.Trim() == string.Empty)
				{
					validatorTop.Visible = true;
					return;
				}
				else
				{
					validatorTop.Visible = false;
					searchTextBottom.Text = searchTextTop.Text;
				}
			}
			else
			{
				if(searchTextBottom.Text.Trim() == string.Empty)
				{
					validatorBottom.Visible = true;
					return;
				}
				else
				{
					validatorBottom.Visible = false;
					searchTextTop.Text = searchTextBottom.Text;
				}
			}
			DataBind();
		}
		#endregion

		#region Private Methods

		private static string[] SectionsToSearchDecode(string stringToDecode) 
		{
			Hashtable galleries = Galleries.GetGalleries(CSContext.Current, CSContext.Current.User.UserID, false, true, false);
			UTF8Encoding decoder = new UTF8Encoding();
			string[] filteredSectionsToSearch = null;
			ArrayList verifiedSectionList = new ArrayList();

			// Convert the parameters from a base64 encoded string and then
			// into a string[]
			if (stringToDecode != string.Empty) 
			{
				// Attempt to decode the string
				try 
				{
					byte[] forumList = Convert.FromBase64String( stringToDecode );
					filteredSectionsToSearch = decoder.GetString( forumList ).Split(',');
				} 
				catch { filteredSectionsToSearch = null; }
			}

			// Get a verified list of only the forums the user has access to
			// if our forum string[] is null we get all the forums the user
			// has access to; otherwise the list is checked to ensure we only
			// get forums the user is allowed to see
			if(filteredSectionsToSearch == null) 
			{
				foreach(Gallery gallery in galleries.Values)
					verifiedSectionList.Add(gallery.SectionID.ToString());
			} 
			else 
			{
				foreach(string appKey in filteredSectionsToSearch) 
				{
					if(galleries[appKey] != null) 
						verifiedSectionList.Add( ((Gallery)galleries[appKey]).SectionID.ToString() );
				}
			}

			return (string[])verifiedSectionList.ToArray(typeof(string));
		}

		#endregion

	}
}

⌨️ 快捷键说明

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