protected void ChangeItemsPerPageAndBind(object sender, EventArgs e) { _hasPageUnitChanged = true; //This fired so it must have changed. // clear the keyword search field txtSWRKeyword.Text = ""; //Get 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. //So we should know the current page number, it was part of the postback. //we do have to know what the old page unit was, that will give us the first record. int firstItem, lastItem; SimpleUlPager.GetFirstItemLastItem(CurrentPage, PreviousItemsPerPage, (int)TotalNumberOfResults, out firstItem, out lastItem); //Set the current page. CurrentPage = firstItem / ItemsPerPage + (firstItem % ItemsPerPage > 0 ? 1 : 0); //If the CurrentPage is 0, then Endeca will throw errors, so we need to set a curretnpage ==0 //to 1. This brings up a very good point, why do we show the change number of items per page //if we have 0 results. One might also ask, why do we go to Endeca to bind the results when we //know they will not change? The answer is partially due to lazyness and making the code simple. //LoadResults not only loads the results, but also makes sure that things like total results and //previousitemsperpage are set. I would rather just call this and not add yet another code //branch to this already complicated code. if (CurrentPage == 0) { CurrentPage = 1; } //Note LoadResults gets the page unit on its own. LoadResults(); }
/// <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. if (CurrentPage == 1 && OldKeywords.Count == 0) { 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... ISiteWideSearchResultCollection results = null; try { results = NCI.Search.SiteWideSearch.GetSearchResults(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 = ContentDeliveryEngineConfig.PageTitle.AppendPageTitle.Title; rptResults.DataSource = from res in results select new NCI.Web.UI.WebControls.TemplatedDataItem( GetSearchResultTemplate((ISiteWideSearchResult)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((ISiteWideSearchResult)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 ************************************************* }