views:

840

answers:

4

I have an aspx page with a gridview. In my page load event, I load a datatable with all the data like so:

HistoricalPricing historicalPricing = new HistoricalPricing();
DataTable dtHistoricalPricing = new DataTable();

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            dtHistoricalPricing = historicalPricing.GetAuctionData();
        }
    }

The above loades the data into the datatable fine. I also have a listbox which contains a list of auctions. When I click on an auction, I use the RowFilter on a DataView to display a Gridview with the data that was selected, but the DataTable seems to loose its value and I can't figure out why. Here is the code below:

protected void lstAuctions_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataView dvPricing = new DataView(dtHistoricalPricing); // Loses Value
        dvPricing.RowFilter = "Auction = 1"; //Hard-Coded for Test
        gvPricing.DataSource = dvPricing.ToTable();
        gvPricing.DataBind();
    }
+1  A: 

I think I figured it out, is it because when I click on the ListBox, it does a postback and I am only loading the data on the first load of the Page? If this is correct, I think I answered my own question.

I put the datatable in Session after loading it on the first Page Load and this seemed to solve my problem. Not sure if this is the best way though.

Xaisoft
That is correct: each time you do a post back you're working with a brand new instance of your page class.
Joel Coehoorn
Um, no, that's not the best way. Just reload the DataTable each time, or put the DataTable into the cache, or, most appropriately, into ViewState.
Robert C. Barth
+1  A: 

Sort of answered your own question. You are creating a new instance of the object every page load, so when the listbox posts a postback, your code is dealing with a different object.

You would be better to declare the object globally, and then instantiate it in the !Postback code eg:

DataTable dtHistoricalPricing = null;

...

if (!Page.IsPostBack)
{
   if (dtHistoricalPosting == null)
   {
       //shouldn't need to do a new dtHistoricalPricing as the method below is returning a new instance?
       dtHistoricalPricing = historicalPricing.GetAuctionData();
   }
}
KiwiBastard
I tried this before and when I get to the SelectedIndexChanged Event, dtHistoricalPricing is null, so it gives me an error when I try to use RowFilter.
Xaisoft
+1  A: 

Every time you do a postback you're dealing with a new instance of your page class. That means a new datatable object as well.

If you really want to persist it between postbacks (and make sure you consider the memory implications for that when you may have 1000 people hitting this web server at the same time) then you can put the datatable in the Session, ViewState, or other location that persists state.

Joel Coehoorn
Is any of those options least memory intensive?
Xaisoft
It depends on what you're storing, how much traffic the server gets, etc. For example: ViewState is _great_ in a small corporate intranet where every has fast upload times, but not so great for serving pages to dial-up users on the public intranet.
Joel Coehoorn
Dial-up users on the public intranet? What's a dial-up user?
Robert C. Barth
D'oh! Meant IntERnet. But you can still have "dialup" users for an intranet, since that's similar to how some kinds of vpns work.
Joel Coehoorn
+1  A: 

It is difficult to persist values between requests in ASP.NET. The most reliable way would be to put it in ViewState, but that will send the whole thing to the client and back, so you shouldn't put much data there. The session is an alternative, but it can become a problem when a user opens several windows with your page within the same session. Then there is also application state and cache, but those are shared among ALL requests (regardless if user). Plus, if you have a web farm, the values in there are local for every server. Even more, IIS can spawn several ASP.NET processes on the same machine too, and they will each have their own Application State. The same can be said about static variables.

Vilx-
Actually, assuming all users get the same drop down list then application state would be great place for this.
Joel Coehoorn