ASP Listview with DataPager using generic List as datasource throws 500 error on second callback

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
<%# Eval("TradeDate") %> <%# Eval("IsActive") %> <%# Eval("WatchType") %> <%# Eval("LiraAmount") %> <%# Eval("DollarAmount") %> <%# Eval("ExchangeRate") %>


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; }
    }
}

}

Tagged:

Posts

  • pgregorypgregory USMember ✭✭

    Here is a readable copy of the markup:

    <%@ Page Language="C#" Inherits="ClockWeb.Currency" %> <!DOCTYPE html> <html> <head runat="server"> <title>Currency</title> </head> <body> <form id="form1" runat="server"> <asp:ListView ID="lvWatches" runat="server" GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging"> <LayoutTemplate> <table cellpadding="0" cellspacing="10px"> <tr> <th>Trade Date</th> <th>Active</th> <th>Watch Type</th> <th>Lira Amount</th> <th>Dollar Amount</th> <th>Exchange Rate</th> </tr> <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder> <tr> <td colspan = "3"> <asp:DataPager ID="DataCurrency1" runat="server" PagedControlID="lvWatches" PageSize="10" > <Fields> <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true" ShowNextPageButton="false" /> <asp:NumericPagerField ButtonType="Link" /> <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton = "false" /> </Fields> </asp:DataPager> </td> </tr> </table> </LayoutTemplate> <GroupTemplate> <tr> <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder> </tr> </GroupTemplate> <ItemTemplate> <td><%# Eval("TradeDate") %></td> <td><%# Eval("IsActive") %></td> <td><%# Eval("WatchType") %></td> <td><%# Eval("LiraAmount") %></td> <td><%# Eval("DollarAmount") %></td> <td><%# Eval("ExchangeRate") %></td> </ItemTemplate> </asp:ListView> </form> </body> </html>

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    This section of the forums is for Xamarin.Mac specific questions and discussion. Unfortunately, I know nothing about ASP.

    I'm going to move this to another section that hopefully might know more.

Sign In or Register to comment.