示例#1
0
        protected void Page_PreRender(object sender, EventArgs e)
        {
            //Get Settings
            if (Request.RequestType == "POST")
            {
            }
            else
            {
                //The method was a GET, therfore we must be paging.
                _currentPage    = Strings.ToInt(Request.Params["PageNum"], 1);
                _recordsPerPage = Strings.ToInt(Request.Params["RecordsPerPage"], 10);
                _offSet         = Strings.ToInt(Request.Params["OffSet"], 0);
            }

            if (PerformSearch)
            {
                //set up the search collection
                //determine what text needs to be removed from the title e.g. - National Cancer Institute
                SiteWideSearchConfig searchConfig = ModuleObjectFactory <SiteWideSearchConfig> .GetModuleObject(SnippetInfo.Data);

                if (searchConfig != null)
                {
                    SearchCollection = searchConfig.SearchCollection;
                }

                if (Keyword != null)
                {
                    //Store keyword in viewstate (This does not check if it is not there already)
                    if (ViewState[swKeyword] == null)
                    {
                        ViewState.Add(swKeyword, Keyword);
                    }
                    else
                    {
                        ViewState[swKeyword] = Keyword;
                    }

                    try
                    {
                        SiteWideSearchAPIResultCollection results = SiteWideSearchManager.Search(SearchCollection, Keyword, _recordsPerPage,
                                                                                                 (_currentPage - 1) * _recordsPerPage);

                        rptSearchResults.DataSource = results.SearchResults;
                        rptSearchResults.DataBind();

                        if (results.ResultCount == 0)
                        {
                            ResultsText = "No results found";
                            rptSearchResults.Visible = false;
                        }
                        else
                        {
                            int startRecord = 0;
                            int endRecord   = 0;
                            _resultsFound = true;
                            SimplePager.GetFirstItemLastItem(_currentPage, _recordsPerPage, (int)results.ResultCount, out startRecord, out endRecord);

                            //phNoResultsLabel.Visible = false;
                            rptSearchResults.Visible = true;
                            string resultsCount = String.Format("{0}-{1} of {2}", startRecord.ToString(), endRecord.ToString(), results.ResultCount.ToString());
                            ResultsText = "Results " + resultsCount;
                        }

                        spPager.RecordCount    = (int)results.ResultCount;
                        spPager.RecordsPerPage = _recordsPerPage;
                        spPager.CurrentPage    = _currentPage;
                        spPager.BaseUrl        = PrettyUrl + "?swKeywordQuery=" + Keyword;
                    }
                    catch (Exception ex)
                    {
                        //capture exactly which keyword caused the error
                        log.Error("Search with the following keyword returned an error: " + Keyword, ex);
                    }
                }
                else
                {
                    ResultsText = "No results found";
                    rptSearchResults.Visible = false;
                }
            }
            else
            {
                ResultsText = String.Empty;
            }
        }
        /// <summary>
        /// This method is the one that sets up label text and binds the search results.
        /// </summary>
        private void LoadResults()
        {
            //Get Bestbets. Only show if we are on page one and we are not doing a search within
            //the results, and if the search term is not empty.
            if (CurrentPage == 1 && OldKeywords.Count == 0 && !string.IsNullOrWhiteSpace(SearchTerm))
            {
                BestBetUIResult[] bbResults = GetBestBetsResults(SearchTerm);

                if (bbResults.Length > 0)
                {
                    isBestBet = true;
                    rptBestBets.DataSource = bbResults;
                    rptBestBets.DataBind();
                    rptBestBets.Visible = true;
                }
                else
                {
                    rptBestBets.Visible = false;
                }
            }
            else
            {
                rptBestBets.Visible = false;
            }

            //Get Results...
            SiteWideSearchAPIResultCollection results = null;

            try
            {
                results = SiteWideSearchManager.Search(SearchCollection, SearchTerm, ItemsPerPage,
                                                       (CurrentPage - 1) * ItemsPerPage);
            }
            catch (Exception e)
            {
                //capture exactly which keyword caused the error
                log.ErrorFormat("Search with the following keyword returned an error: {0}", e, KeywordText);
            }

            //Set the last total number of results so if the user changes the ItemsPerPage(pageunit)
            //then we can move them to the closest page.  Say you are viewing 10 items per page and
            //you are on page 6. This shows 51-60.  If you change to 50 items per page you should put
            //the user on page 2 and not page 1.  That way they will see 51-100.
            TotalNumberOfResults = results.ResultCount;
            PreviousItemsPerPage = ItemsPerPage;

            int firstIndex, lastIndex;

            //Get first index and last index
            SimpleUlPager.GetFirstItemLastItem(CurrentPage, ItemsPerPage, (int)results.ResultCount, out firstIndex, out lastIndex);
            _resultOffset = firstIndex;

            if (results != null && results.ResultCount > 0)
            {
                //the title text that needs to be removed from the search result Title
                string removeTitleText = ResultTitleText;
                if (string.IsNullOrWhiteSpace(removeTitleText))
                {
                    removeTitleText = ContentDeliveryEngineConfig.PageTitle.AppendPageTitle.Title;
                }

                rptResults.DataSource = from res in results.SearchResults
                                        select new NCI.Web.UI.WebControls.TemplatedDataItem(
                    GetSearchResultTemplate(res),
                    new
                {
                    URL         = res.Url,
                    Title       = (!string.IsNullOrEmpty(removeTitleText) && res.Title.Contains(removeTitleText)) ? res.Title.Remove(res.Title.IndexOf(removeTitleText)) : res.Title,
                    DisplayUrl  = res.Url,
                    Description = res.Description,
                    Label       = GetSearchResultLabel(res),
                });
                rptResults.DataBind();
            }

            //Set Keywords in labels
            lblResultsForKeyword.Text         = KeywordText;
            lblTopResultsXofYKeyword.Text     = KeywordText;
            lblSearchWithinResultKeyword.Text = KeywordText;

            //Show labels for results X of Y
            ShowResultsXoYLabels(firstIndex, lastIndex, results.ResultCount);

            //Setup pager
            SetupPager();

            //Set the Search Within Results Radio button to new search
            rblSWRSearchType.SelectedIndex = 0;

            //// Web Analytics *************************************************
            // Add number of search results to analytics
            this.PageInstruction.SetWebAnalytics(WebAnalyticsOptions.eVars.evar10, wbField =>
            {
                wbField.Value = TotalNumberOfResults.ToString();
            });

            this.PageInstruction.AddFieldFilter("channelName", (name, data) =>
            {
                data.Value = "NCI Home";
            });

            // Add Best Bets event to analytics
            if (rptBestBets.Visible)
            {
                this.PageInstruction.SetWebAnalytics(WebAnalyticsOptions.Events.event10, wbField =>
                {
                    wbField.Value = WebAnalyticsOptions.Events.event10.ToString();
                });
            }
            //// End Web Analytics *************************************************
        }