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

Posted on 2006-06-06
Last Modified: 2008-03-17
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?
Question by:chuang4630
    LVL 4

    Expert Comment

    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)
    LVL 17

    Expert Comment

    svy the ViewState do not take effect in navigation from page to page
    LVL 4

    Expert Comment

    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?
    LVL 4

    Expert Comment

    Ok, it's GridView paging we're talking about... Sorry.
    Are you implementing OnPageIndexChanged event handler?
    LVL 1

    Author Comment

    I implemented PageIndexChanging event handler.

    What should I do with PageIndexChanged event handler?
    LVL 4

    Accepted Solution

    protected void PageIndexChanging(object o, GridViewPageEventArgs e)
    grid.PageIndex = e.NewPageIndex;
    grid.DataSource = dataSource;

    again I would cache the dataSource.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now