2

I am trying to populate my gridview with amazon search results. At the moment when the page loads, datasource is filled with data. What I am trying to do is show the data after pressing the Search button, but it displays "No Records Found". I have tried many different ways, the only way it worked, was without postback, but then the issue was that every time I changed the page on the gridview, GetProducts("Playstation") command was initiated over again.

The solution I have been searcing for: Load the page -> click button -> fill the gridview with data -> When choosing new page in gridview, data is displayed but Getproducts("Playstation") is not initiated again.

Is there a way to do this ?

protected void Page_Load(object sender, EventArgs e) {
  Button1.Click += new EventHandler(this.GreetingBtn_Click);
  if (!Page.IsPostBack) {
    AmazonSearch us = new Amazon.PAAPI.AmazonSearch();
    GridView1.DataSource = us.GetProducts("Playstation");
  }
}
void Search(Object sender, EventArgs e) {
  Button clickedButton = (Button) sender;
  GridView1.DataBind();
}
protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e) {
  GridView1.PageIndex = e.NewPageIndex;
  GridView1.DataBind();
}

EDIT

I figured it out thanks to FastGeeks anwser. I added variable ds to the code. and made the following changes:

DataSet ds = new DataSet();

protected void Page_Load(object sender, EventArgs e) {
  Button1.Click += new EventHandler(this.GreetingBtn_Click);
 }
void Search(Object sender, EventArgs e) {
  Button clickedButton = (Button) sender;
  AmazonSearch us = new Amazon.PAAPI.AmazonSearch();
  ds.Tables.Add(us.GetProducts("Playstation"));
  GridView1.DataSource = ds;
  Session["ds"] = ds;
  GridView1.DataBind();
}
protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e) {
  GridView1.PageIndex = e.NewPageIndex;
  ds = (DataSet)Session["ds"];
  GridView1.DataSource = ds;
  GridView1.DataBind();;
}
Suprsonic
  • 135
  • 1
  • 7

2 Answers2

3

You need to assign DataSource again in PageIndexChanging before binding it.

protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataSource = us.GetProducts("Playstation");
    GridView1.DataBind();
}

Similarly assign DataSource in search method too.

void Search(Object sender,  EventArgs e)
{
    Button clickedButton = (Button)sender;
    GridView1.DataSource = us.GetProducts("Playstation");
    GridView1.DataBind();
}
Adil
  • 146,340
  • 25
  • 209
  • 204
  • But this way, I will perform search from amazon again, ain't there a way I could bind the data somewhere(i.e. database) because searching from amazon every time I change the page takes 1-2 seconds. – Suprsonic Apr 06 '13 at 16:30
1

My take on this would be to store the results from the Amazon search into a DataTable, and then store the DataTable in a session variable; this is entirely possible because a DataTable is Serializable and will store in the session.

Then in your grid_PageIndexChanging event, and Search method you can retrieve the DataTable from session and re-assign your data without repeating the Amazon search.

FastGeek
  • 411
  • 2
  • 7
  • Thank you! This worked for me, I made a Dataset variable. I add data there when the search button is clicked. – Suprsonic Apr 07 '13 at 11:06