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();;
}
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();
}
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.
Related
My gridview row updating button is not working when I press edit and then press on the button to apply the changes it doesn't do anything. Am I missing something?
My code looks like this
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string afspraak = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditAfspraak"))).Text;
string uitleg = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditUitleg"))).Text;
int id = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditID"))).Text);
_controller.RecordUpdatenTblAfspraken(afspraak, uitleg, id);
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
}
ASP.NET
Couldn't paste it correctly so it's here:
https://pastebin.com/S3S09nLL
Highlighted button isn't working
You have to rebind the grid after you change the EditIndex.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = mijnDatabaseBron;
GridView1.DataBind();
}
The same goes for GridView1_RowCancelingEdit and when update has succeeded in GridView1_RowUpdating. This is assuming you added the OnRowEditing, OnRowCancelingEdit and OnRowUpdating events to the GridView.
Also as user7415073 commented, the binding of the GridView must be inside an IsPostBack check.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = mijnDatabaseBron;
GridView1.DataBind();
}
}
And see my answer here as to how you can easily pass the EditID to code behind.
Get old value of label inside GridView during RowUpdating (ASP.NET GridView) C#
This might be a easy problem but i don't see what i'm doing wrong here. The code is:
protected void dgvSelected_RowCommand (object sender, GridViewCommandEventArgs e)
{
if(e.CommandName =="Delete")
{
if (!string.IsNullOrEmpty(e.CommandArgument.ToString()))
{
int RowIndex = Convert.ToInt32(e.CommandArgument);
dgvSelected.DeleteRow(RowIndex);
dgvSelected.DataBind();
}
}
}
And at the end of the class i have this method.
protected void dgvSelected_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
I'm using asp .net and i have a gridview that several rows and at each row i have a delete button where the user can press it to delete the row. For the moment when i press the delete button the whole gridview disappears. What am i doing wrong?
Try this
protected void dgvSelected_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
dgvSelected.DataBind();
}
Since the gridview data was from antoher gridview, the solution was to make a viewstate variable and save the datatable in it. And then to delete one row:
protected void dgvSelected_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["SelectedValue"] as DataTable;
dt.Rows[index].Delete();
ViewState["SelectedValue"] = dt;
dgvSelected.DataSource = dt;
dgvSelected.DataBind();
}
I have a detailsview inside a modalpopup that is supposed to be displaying the selected row in my gridivew, but it is only displaying the first row in my gridview. What am I doing incorrectly? Thank you for your help.
protected void gvStudent_SelectedIndexChanged(object sender, EventArgs e)
{
this.dvstudent.Visible = true;
this.dvstudent.DataBind();
this.UpdatePanelDetailsView1.Update();
this.modalstudent.Show();
}
protected void Selecttomodifystudent_Click(object sender, EventArgs e)
{
LinkButton buttonstudent = sender as LinkButton;
GridViewRow row = (GridViewRow)buttonstudent.NamingContainer;
this.sqlModifyStudent1.SelectParameters.Clear();
this.dvstudent.DataBind();
this.UpdatePanelDetailsView1.Update();
this.modalstudent.Show();
}
I found the answer to my problem. It was right before my eyes:
protected void gvStudent_SelectedIndexChanged(object sender, EventArgs e)
{
dvstudent.PageIndex = gvStudent.SelectedIndex;
}
I have a RadioButtonList and a ListBox. I have bound RadioButtonList to database.
Therefore upon selecting an item in the RadioButtonList, I want to retrieve some data into the ListBox. The code I have tried is :
protected void Page_Load(object sender, EventArgs e)
{
RadioFill();
}
public void RadioFill()
{
SqlDataAdapter mydata = new SqlDataAdapter("SELECT DISTINCT Param_Name FROM Parameter_Value_Master", con);
DataSet dset = new DataSet();
mydata.Fill(dset, "Table");
RadioButtonList1.Items.Clear();
RadioButtonList1.DataSource = dset.Tables[0];
RadioButtonList1.DataTextField = dset.Tables[0].Columns["Param_Name"].ColumnName;
RadioButtonList1.DataBind();
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlDataAdapter mydata = new SqlDataAdapter("SELECT Value_Option FROM Parameter_Value_Master", con);
DataSet dset = new DataSet();
mydata.Fill(dset, "Table");
ListBox1.Items.Clear();
ListBox1.DataSource = dset.Tables[0];
ListBox1.DataTextField = dset.Tables[0].Columns["Value_Option"].ColumnName;
ListBox1.DataBind();
}
The issue I am facing here is upon selecting an item, the whole panel in which I have placed both my RadioButtonList and ListBox goes invisible.
Kindly help...!! Thankyou...!!
First, change Page_Load method as:
protected void Page_Load(object sender, EventArgs e)ยจ
{
if (!Page.IsPostBack)
{
RadioFill();
}
}
If it not help than post code from your *.aspx file.
Remark: The method RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e),
there is not selecting based on radio button list selected value.
I have a GridView control which I fill thru c# code and want to do paging with code like this
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
BindGrid();
GridView1.PageIndex = e.NewPageIndex;
GridView1.SelectedIndex = -1;
}
The BindGrid() function is where I get the data source for the grid and bind it.
It all works nicely exept that I have to press twice in order for it to change pages.
What can I do so that it will page after one click?
Thank you.
Your binding order is not correct.. It should be like...
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.SelectedIndex = -1;
BindGrid(); // Call bind here
}