Question

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();;
}
Was it helpful?

Solution 2

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.

OTHER TIPS

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top