I'm building a simple ASP page with a listview and datapager.
In my code-behind I'm setting the datasource to a generic List - a custom class with public properties.
It populates great on the page load and displays the pages properly.
I navigate to the second page and again, it populates and displays the data properly.
I navigate to the third page and I get:
System.InvalidCastException
Specified cast is not valid.
Description: HTTP 500.Error processing request.
Details: Non-web exception. Exception origin (name of application or object): System.Web.
Exception stack trace:
at System.Web.UI.StateBag.LoadViewState (System.Object savedState) [0x00007] in /private/tmp/source-mono-4.8.0/bockbuild-mono-4.8.0-branch/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/System.Web/System.Web.UI/StateBag.cs:85
I must be missing something easy. Here is the markup:
<%@ Page Language="C#" Inherits="ClockWeb.Currency" %>
<!DOCTYPE html>
Currency
Trade Date | Active | Watch Type | Lira Amount | Dollar Amount | Exchange Rate |
---|---|---|---|---|---|
And the code-behind
using System;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
namespace ClockWeb
{
public partial class Currency : System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (!IsPostBack) {
lvWatches.DataSource = LoadData ();
lvWatches.DataBind ();
}
}
private List<CurrencyWatch> LoadData ()
{
var watches = new List<CurrencyWatch> ();
string filename;
if (File.Exists ("Currency/currency_settings.txt")) {
filename = "Currency/currency_settings.txt";
} else {
filename = "/home/debian/clock-radio/currency/currency_settings.txt";
}
if (File.Exists (filename)) {
var json = File.ReadAllText (filename);
watches = new JavaScriptSerializer ().Deserialize<List<CurrencyWatch>> (json);
watches.Sort ((x, y) => DateTime.Parse (x.TradeDate).CompareTo (DateTime.Parse (y.TradeDate)));
}
return watches;
}
protected void OnPagePropertiesChanging (object sender, PagePropertiesChangingEventArgs e)
{
var pager = (lvWatches.FindControl ("DataCurrency1") as DataPager);
pager.SetPageProperties (e.StartRowIndex, e.MaximumRows, false);
lvWatches.DataSource = LoadData ();
lvWatches.DataBind ();
}
public class CurrencyWatch
{
public string TradeDate { get; set; }
public string IsActive { get; set; }
public string WatchType { get; set; }
public string LiraAmount { get; set; }
public string DollarAmount { get; set; }
public string ExchangeRate { get; set; }
}
}
}