Persisting DataSource in GridView after PostBack
Asked Answered
S

3

7

So I have a GridView in my ASPX page.

When I click <asp:Button id="btnBindIt" runat="server" /> it binds a datatable as follows:

theDataTable = GetAllTheRecords();
gvTheGridView.DataSource = theDataTable;
gvTheGridView.DataBind();

Note: theDataTable is a member

private DataTable theDataTable;

This works as expected.

Now, after the GridView is displayed nicely, I want to export the data to CSV, so I now click the <asp:Button id="btnExportIt" runat="server" /> which runs the code:

exportToCsv(theDataTable);

but theDataTable is null.

So I tried

exportToCsv(gvTheGridView.DataSource)

Which is also null.

What's the standard way of persisting this data? I don't really want to hit the DB again as it's quite a long SPROC and the user has already waited once.

Thanks in advance!

Susy answered 25/5, 2011 at 4:26 Comment(0)
S
-1

Thanks for the answers everyone.

I steer clear of putting stuff into the VeiwState or session unnecessarily, so I think the best way to persist this data is to Cache it.

I think the MemoryCache is the most appropriate place for this, is is how I ended up implementing it.

Susy answered 2/4, 2012 at 7:34 Comment(2)
-1: #15408617Syriac
I think it's a bad idea to store large amounts of data like this in the session. For me, my solution was to not store the results that I planned on databinding, but to make the query that got the results faster by caching. It worked well for my situation.Susy
M
11

Class level variables can't maintain their value on postback.

But there are two ways you can maintain Data on the Page's PostBack: ViewState and Session State. But I would suggest in your Scenario to put it in the ViewState.

ViewState["theDataTable"] = theDataTable;
Session["theDataTable"] = theDataTable;

And then you can access it on the page postback:

DataTable theDataTable = (DataTable)ViewState["theDataTable"];
DataTable theDataTable = (DataTable)Session["theDataTable"];
Maladjustment answered 25/5, 2011 at 4:29 Comment(3)
Thank Muhammed. I'm concerned with using the session or viewstate with a large dataset. Would you recommend doing this for a large dataset?Susy
What if you export Gridview data to CSV ?Maladjustment
Another way is, you can iterate your Gridview Rows on server and extract DataTable from Gridview and then export it csv.Maladjustment
C
5

Declare the datatable as follows and everything will work as expected

    private string _theDataTable="theDataTable";
    private DataTable theDataTable
    {
            get
            {
                    if(ViewState[_theDataTable]==null)
                            return new DataTable();
                    return (DataTable)ViewState[_theDataTable];
            }
            set
            {
                    ViewState[_theDataTable] = value;
            }
    }

cheers!

Crept answered 25/5, 2011 at 6:23 Comment(1)
instead of writing return new DataTable(); in case of ViewState being null, it would be better to get fresh data from the data source (database or what ever it is).Cattery
S
-1

Thanks for the answers everyone.

I steer clear of putting stuff into the VeiwState or session unnecessarily, so I think the best way to persist this data is to Cache it.

I think the MemoryCache is the most appropriate place for this, is is how I ended up implementing it.

Susy answered 2/4, 2012 at 7:34 Comment(2)
-1: #15408617Syriac
I think it's a bad idea to store large amounts of data like this in the session. For me, my solution was to not store the results that I planned on databinding, but to make the query that got the results faster by caching. It worked well for my situation.Susy

© 2022 - 2024 — McMap. All rights reserved.