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(); }
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.