• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

How do you preserve the data while navigating from page to page in GridView control?

I need to implement a GridView control in ASP.NET 2.0. To minimize the data query overhead, I prefer to query data once.
Page view is enabled.
However, it seems necessary to re-assign the datasource to the GridView control everytime the page is postback. How do I preserve the data while navigating from page to page?
0
chuang4630
Asked:
chuang4630
  • 4
1 Solution
 
svyCommented:
If GridView ViewState is enabled there's no need to re-assign the datasource. You have databind the grid only once when IsPostBack is false. If data bound to datagrid changes as a result of your form's processing only then you would have to rebind the gridview's datasource.

This code in Page_PreRender (or in Page_Load if you're not using the prerender)
if(!IsPostBack){
gridView.DataSouce=myDataSource;
gridView.DataBind();
}
0
 
Elvio Lujan.Net Senior DeveloperCommented:
svy the ViewState do not take effect in navigation from page to page
0
 
svyCommented:
Page to Page? ViewState of a control is specific to it's parent page. Control A on page A is not the same as control A on page B. It may be the same class but it's not the same object.
Do you have a GridView on different pages using the same datasource? If so perhaps, caching the datasource in Session or Cache depending on the data, is what you're looking for. How about a user web control that encapsulates GridView and manages it's datasource?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
svyCommented:
Ok, it's GridView paging we're talking about... Sorry.
Are you implementing OnPageIndexChanged event handler?
0
 
chuang4630Author Commented:
I implemented PageIndexChanging event handler.

What should I do with PageIndexChanged event handler?
0
 
svyCommented:
protected void PageIndexChanging(object o, GridViewPageEventArgs e)
{
grid.PageIndex = e.NewPageIndex;
grid.DataSource = dataSource;
grid.DataBind();
}

again I would cache the dataSource.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now