/* * This function is called when prevous or next is clicked * It grabs the results of the page that you are moving to * and calls populateHtml to display the results. */ public void Tabify(bool isprevious) { //when previous is clicked and you are not on the first page then go to previous tab if (isprevious && Convert.ToInt32(Session["Page"]) != 0) { Session["Page"] = Convert.ToInt32(Session["Page"]) - 1; } //when next is clicked then go to the next tab of results else if (!isprevious) { Session["Page"] = Convert.ToInt32(Session["Page"]) + 1; } string query = api + Session["Query"] + "&start=" + (Convert.ToInt32(Session["Page"]) * 10 + 1).ToString(); string json = string.Empty; try { //get the results of the new tab json = new WebClient().DownloadString(query); SearchObject.Rootobject searchObj = JsonConvert.DeserializeObject <SearchObject.Rootobject>(json); var results = Get_Results(searchObj); populateHtml(results); } catch { } UpdateInputs(); }
/* * This function gets results from google api and stores it as list of objects * and calls the populateHtml function. */ protected void Search(object sender, EventArgs e) { if (!string.IsNullOrWhiteSpace(SearchQuery.Text)) { Session["Page"] = 0; Session["Query"] = SearchQuery.Text.Replace(' ', '+'); string query = api + Session["Query"]; string json = string.Empty; try { json = new WebClient().DownloadString(query); SearchObject.Rootobject searchObj = JsonConvert.DeserializeObject <SearchObject.Rootobject>(json); results = Get_Results(searchObj); Session["MaxPageCount"] = Math.Ceiling(Convert.ToDecimal(searchObj.searchInformation.totalResults) / 10); if (Convert.ToInt32(searchObj.searchInformation.totalResults) != 0) { totalResults.Visible = true; totalResults.Text = $"Total Results: {searchObj.searchInformation.totalResults}"; } else { totalResults.Text = $"No Results Found"; } populateHtml(results); } catch { } UpdateInputs(); } }
/* * This function takes the objects generated from the json file and * compresses them into new objects with only the required fields. */ protected List <Result> Get_Results(SearchObject.Rootobject searchObj) { //Extract all of the necessary result specific data from the search result object List <Result> value = new List <Result>(); try { if (searchObj != null) { if (searchObj.items != null) { for (int i = 0; i < searchObj.items.Length; i++) { Result retrieved = new Result(searchObj.items[i].title, searchObj.items[i].link, searchObj.items[i].htmlSnippet , searchObj.items[i].displayLink); value.Add(retrieved); } } } } catch (NullReferenceException ex) { return(new List <Result>()); } return(value); }