search.aspx.cs

来自「ASP.net网站开发四“酷”全书:新闻、论坛、电子商城、博客_源码」· CS 代码 · 共 46 行

CS
46
字号
using BookShop.BLL;
using BookShop.Web.Controls;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.Caching;

namespace BookShop.Web
{
	public class Search : Page
	{
		protected NavBar header;
		protected SimplePager products;

		private void InitializeComponent() {}

		protected void PageChanged(object sender, DataGridPageChangedEventArgs e) {
			products.CurrentPageIndex = e.NewPageIndex;

			// Get the search terms from the query string
			string searchKey = WebComponents.CleanString.InputText(Request["keywords"], 100);

			if (searchKey != ""){

				// Create a data cache key
				string cacheKey = "search" + searchKey;

				// Check if the objects are in the cache
				if(Cache[cacheKey] != null){
					products.DataSource = (IList)Cache[cacheKey];
				}else{
					// If that data is not in the cache then use the business logic tier to fetch the data
					Product product = new Product();
					IList productsBySearch = product.GetProductsBySearch(searchKey);
					// Store the results in a cache
					Cache.Add(cacheKey, productsBySearch, null, DateTime.Now.AddHours(12), Cache.NoSlidingExpiration , CacheItemPriority.High, null);
					products.DataSource = productsBySearch;
				}

				// Databind the data to the controls
				products.DataBind();
			}
		}
	}
}

⌨️ 快捷键说明

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