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?
Who is Participating?
protected void PageIndexChanging(object o, GridViewPageEventArgs e)
grid.PageIndex = e.NewPageIndex;
grid.DataSource = dataSource;

again I would cache the dataSource.
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)
Elvio Lujan.Net Senior DeveloperCommented:
svy the ViewState do not take effect in navigation from page to page
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.

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?
Ok, it's GridView paging we're talking about... Sorry.
Are you implementing OnPageIndexChanged event handler?
chuang4630Author Commented:
I implemented PageIndexChanging event handler.

What should I do with PageIndexChanged event handler?
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.