Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi I am facing a data refresh problem in asp.net.
On the home page when a link is clicked another page is loaded which has a gridview control.This gridview displays data from an xml file and it has edit/update options in it.
When the value of any cell is edited and updated the new value is displayed in the gridview and also successfully written back to the xml in the back end.But the problem is that if the linked is clicked again the grid view shows the old data rather than the updated data.

Can someone please help me out.

What I have tried:

1.I have tried using EnableRowsCache="false"in the aspx file,it didn't help.
2.Tried the below code ,but it causes exception when the edit button in the gridview is clicked
protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        MyGridView.DataBind();
    }
}
Posted
Updated 22-Sep-16 22:00pm
v2
Comments
Karthik_Mahalingam 22-Sep-16 7:30am    
assign the datasource
Member 3864348 22-Sep-16 7:49am    
ata();
}
}

And LoadData() has been defined as below:-

protected void LoadData()
{
// Creates a data table and populate it from data from xml file.Have just removed the data table population code to keep this post short.
DataTable dt = new DataTable()
MyGridView.DataSource = dt;
MyGridView.DataBind();
}

So,DatBind() is already called but no luck.
When I click on the link(menu item on the home page) again after I modify the data in the gridview,it still shows the older data (however the data in the backend xml file is being updated)
Karthik_Mahalingam 22-Sep-16 7:52am    
place break point and check the datatabe contains data
Member 3864348 22-Sep-16 7:55am    
There is no database involved in it,the data being shown in the gridview are from an XML file.

It should be not IsPostback
Check following-
C#
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack) // note the not operator (!)
   {
      MyGridView.DataBind();
   }
} 


Hope, it helps :)
 
Share this answer
 
Comments
Member 3864348 22-Sep-16 7:25am    
Hi,
Thanks for you reply.
I already have a method named LoadData() which is getting called as

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}

and inside LoadData() method the MyGridView.DataBind() is called.But this has not helped me.
Suvendu Shekhar Giri 22-Sep-16 7:29am    
Can you share the relevant code i.e,
--LoadData()
--edit button click event
Member 3864348 22-Sep-16 7:51am    
This is how the LoadData() looks

protected void LoadData()
{
// Creates a data table and populate it from data from xml file.have just removed the data table population code to keep this post short.
DataTable dt = new DataTable()
MyGridView.DataSource = dt;
MyGridView.DataBind();
}
Suvendu Shekhar Giri 22-Sep-16 7:54am    
This potion looks perfectly fine. Can you also share the link/button click event code?
Member 3864348 22-Sep-16 8:04am    
protected void MyGridView_RowUpdating(object sender,GridViewUpdateEventArgs e)
{
TextBox tbProcessName =(TextBox)MyGridView.Rows[e.RowIndex].FindControl("txtProcessName");

TextBox tbStatus = (TextBox)MyGridView.Rows[e.RowIndex].FindControl("txtStatus");

string ModifiedProcessName = tbProcessName.Text;
string ModifiedStatus = tbStatus.Text;

// Then code to write the changed data back to the xml file.

MyGridView.EditIndex = -1;
LoadData();
}
You need to set the DataSource before calling DataBind() to reflect the changes.

C#
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack) 
   {
      MyGridView.DataSource = ???; //set the data source here
      MyGridView.DataBind();
   }
} 
 
Share this answer
 
Comments
Member 3864348 22-Sep-16 7:48am    
Hi,

My Page_load() method is as below:-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}

And LoadData() has been defined as below:-

protected void LoadData()
{
// Creates a data table and populate it from data from xml file.Have just removed the data table population code to keep this post short.
DataTable dt = new DataTable()
MyGridView.DataSource = dt;
MyGridView.DataBind();
}

So,DatBind() is already called but no luck.
When I click on the link(menu item on the home page) again after I modify the data in the gridview,it still shows the older data (however the data in the backend xml file is being updated)

Could you plz help me out.
Vincent Maverick Durano 22-Sep-16 8:55am    
I would suggest you to debug your code, set a break point at LoadData() method and then step into it to figure out what went wrong.
It was a cache problem indeed.

Calling
C#
Response.Cache.SetCacheability(HttpCacheability.NoCache) in Page_Load() fixed the refresh issue.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900