protected void BindProductList()
        {
            IList <Product> products = ProductDataSource.AdvancedSearch(_keywords, this._categoryId, _manufacturerId, true, true, true, 0, 0, PageHelper.GetShopByChoices(), _pageSize, (_hiddenPageIndex * _pageSize), SortResults.SelectedValue);

            // DELAYED QUERIES TO EAGER LOAD RELATED DATA FOR PERFORMANCE BOOST
            if (products.Count < 415)
            {
                List <int> ids = products.Select(p => p.Id).ToList <int>();

                var futureQuery = NHibernateHelper.QueryOver <Product>()
                                  .AndRestrictionOn(p => p.Id).IsIn(ids)
                                  .Fetch(p => p.Specials).Eager
                                  .Future <Product>();

                NHibernateHelper.QueryOver <Product>()
                .AndRestrictionOn(p => p.Id).IsIn(ids)
                .Fetch(p => p.ProductOptions).Eager
                .Future <Product>();

                NHibernateHelper.QueryOver <Product>()
                .AndRestrictionOn(p => p.Id).IsIn(ids)
                .Fetch(p => p.ProductKitComponents).Eager
                .Future <Product>();

                NHibernateHelper.QueryOver <Product>()
                .AndRestrictionOn(p => p.Id).IsIn(ids)
                .Fetch(p => p.ProductTemplates).Eager
                .Future <Product>();

                NHibernateHelper.QueryOver <Product>()
                .AndRestrictionOn(p => p.Id).IsIn(ids)
                .Fetch(p => p.Reviews).Eager
                .Future <Product>();

                futureQuery.ToList();
            }

            ProductList.DataSource = products;
            ProductList.DataBind();
            NoSearchResults.Visible = (_searchResultCount == 0);

            if (_pageSize == 0)
            {
                _pageSize = products.Count;
            }

            int minimumPageSize = AlwaysConvert.ToInt(PageSizeOptions.Items[0].Value);
            int totalResults    = ProductDataSource.AdvancedSearchCount(_keywords, this._categoryId, _manufacturerId, true, true, true, 0, 0, false, PageHelper.GetShopByChoices());

            PageSizePanel.Visible = totalResults > minimumPageSize;
        }
예제 #2
0
        protected void Page_Load(object sender, System.EventArgs e)
        {
            string kwords = KeywordField.Text;

            if (string.IsNullOrEmpty(kwords))
            {
                kwords = HiddenSearchKeywords.Value;
            }
            if (Page.IsPostBack && !string.IsNullOrEmpty(kwords))
            {
                _keywords = StringHelper.StripHtml(kwords).Trim();
                HiddenSearchKeywords.Value = _keywords;
            }
            else
            {
                _keywords = Server.UrlDecode(Request.QueryString["k"]);
                if (!string.IsNullOrEmpty(_keywords))
                {
                    _keywords = StringHelper.StripHtml(_keywords).Trim();
                }
            }

            if (HasSearchCriteria())
            {
                //has something to search
                ResultsPanel.Visible          = true;
                NoSearchCriteriaPanel.Visible = false;
                ResultTermMessage.Visible     = true;

                if (!Page.IsPostBack)
                {
                    //initialize search sorting and paging criteria based on query string parameters
                    HiddenPageIndex.Value = AlwaysConvert.ToInt(Request.QueryString["p"]).ToString();
                    string tempSort = Request.QueryString["s"];
                    if (!string.IsNullOrEmpty(tempSort))
                    {
                        ListItem item = SortResults.Items.FindByValue(tempSort);
                        if (item != null)
                        {
                            item.Selected = true;
                        }
                    }
                }

                //initialize paging vars
                _hiddenPageIndex   = AlwaysConvert.ToInt(HiddenPageIndex.Value);
                _searchResultCount = ProductDataSource.AdvancedSearchCount(_keywords, _categoryId, _manufacturerId, true, true, true, 0, 0);
                _lastPageIndex     = ((int)Math.Ceiling(((double)_searchResultCount / (double)_pageSize))) - 1;
                if (_hiddenPageIndex > _lastPageIndex)
                {
                    _hiddenPageIndex = _lastPageIndex;
                }
                if (_pageSize == 0)
                {
                    _pageSize = _searchResultCount;
                }

                //update the header search criteria message
                StringBuilder sb = new StringBuilder();
                if (!string.IsNullOrEmpty(_keywords))
                {
                    sb.Append(string.Format(" for '{0}'", _keywords));
                }
                Category category = CategoryDataSource.Load(_categoryId);
                if (category != null)
                {
                    sb.Append(string.Format(" in '{0}'", category.Name));
                }
                Manufacturer manufacturer = ManufacturerDataSource.Load(_manufacturerId);
                if (manufacturer != null)
                {
                    sb.Append(string.Format(" in '{0}'", manufacturer.Name));
                }
                ResultTermMessage.Text = string.Format(ResultTermMessage.Text, sb.ToString());

                //bind search results
                int startIndex = (_hiddenPageIndex * _pageSize);
                if (startIndex < 0)
                {
                    startIndex = 0;
                }
                var products = ProductDataSource.AdvancedSearch(_keywords, _categoryId, _manufacturerId, true, true, true, 0, 0, _pageSize, startIndex, SortResults.SelectedValue);

                // DELAYED QUERIES TO EAGER LOAD RELATED DATA FOR PERFORMANCE BOOST
                if (products.Count < 415)
                {
                    List <int> ids = products.Select(p => p.Id).ToList <int>();

                    var futureQuery = NHibernateHelper.QueryOver <Product>()
                                      .AndRestrictionOn(p => p.Id).IsIn(ids)
                                      .Fetch(p => p.Specials).Eager
                                      .Future <Product>();

                    NHibernateHelper.QueryOver <Product>()
                    .AndRestrictionOn(p => p.Id).IsIn(ids)
                    .Fetch(p => p.ProductOptions).Eager
                    .Future <Product>();

                    NHibernateHelper.QueryOver <Product>()
                    .AndRestrictionOn(p => p.Id).IsIn(ids)
                    .Fetch(p => p.ProductKitComponents).Eager
                    .Future <Product>();

                    NHibernateHelper.QueryOver <Product>()
                    .AndRestrictionOn(p => p.Id).IsIn(ids)
                    .Fetch(p => p.ProductTemplates).Eager
                    .Future <Product>();

                    futureQuery.ToList();
                }

                ProductList.DataSource = products;
                ProductList.DataBind();

                NoResultsPanel.Visible = _searchResultCount == 0;
                SortPanel.Visible      = _searchResultCount > 1;

                //bind paging
                int totalPages;
                if (_pageSize <= 0)
                {
                    totalPages = 1;
                }
                else
                {
                    totalPages = (int)Math.Ceiling((double)_searchResultCount / _pageSize);
                }

                if (_lastPageIndex > 0)
                {
                    PagerPanel.Visible    = true;
                    PagerPanelTop.Visible = true;
                    List <PagerLinkData> pagerLinkData = GetPagingLinkData(totalPages);
                    PagerControls.DataSource = pagerLinkData;
                    PagerControls.DataBind();
                    PagerControlsTop.DataSource = pagerLinkData;
                    PagerControlsTop.DataBind();

                    PagerMessageTop.Text    = string.Format("Page {0} of {1}", (_hiddenPageIndex + 1), totalPages);
                    PagerMessageBottom.Text = string.Format("Page {0} of {1}", (_hiddenPageIndex + 1), totalPages);
                }
                else
                {
                    PagerPanel.Visible    = false;
                    PagerPanelTop.Visible = false;
                }
            }
            else
            {
                //no search criteria
                ResultsPanel.Visible          = false;
                NoSearchCriteriaPanel.Visible = true;
                ResultTermMessage.Visible     = false;
            }
        }