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?
LVL 1
chuang4630Asked:
Who is Participating?
 
svyCommented:
protected void PageIndexChanging(object o, GridViewPageEventArgs e)
{
grid.PageIndex = e.NewPageIndex;
grid.DataSource = dataSource;
grid.DataBind();
}

again I would cache the dataSource.
0
 
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.