Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Repeater and DataBinder.Eval

I've got a <asp:Repeater> in my webpage, which is bound to a programatically created dataset.

The purpose of this repeater is to create an index from A-Z, which, when clicked, refreshes the information on the page.

The repeater has a link button like so:

<asp:LinkButton ID="indexLetter" Text='<%#DataBinder.Eval(Container.DataItem,"letter")%>'
runat="server"   CssClass='<%#DataBinder.Eval(Container.DataItem, "cssclass")%>'
Enabled='<%#DataBinder.Eval(Container.DataItem,"enabled")%>'></asp:LinkButton>

The dataset is created the following way:

protected DataSet getIndex(String index)
    {
        DataSet ds = new DataSet();
        ds.Tables.Add("index");
        ds.Tables["index"].Columns.Add("letter");
        ds.Tables["index"].Columns.Add("cssclass");            
        ds.Tables["index"].Columns.Add("enabled");
        char alphaStart = Char.Parse("A");
        char alphaEnd = Char.Parse("Z");
        for (char i = alphaStart; i <= alphaEnd; i++)
        {
            String cssclass="", enabled="true";
            if (index == i.ToString())
            {
                cssclass = "selected";
                enabled = "false";
            }
            ds.Tables["index"].Rows.Add(new Object[3] {i.ToString(),cssclass,enabled });
        }
        return ds;
}

However, when I run the page, a "Specified cast is not valid exception" is thrown in Text='<%#DataBinder.Eval(Container.DataItem,"letter")'. I have no idea why, I have tried manually casting to String with (String), I've tried a ToString() method, I've tried everything.

Also, if in the debugger I add a watch for DataBinder.Eval(Container.DataItem,"letter"), the value it returns is "A", which according to me, should be fine for the Text Property.

EDIT:

Here is the exception:

System.InvalidCastException was unhandled by user code
Message="Specified cast is not valid." Source="App_Web_cmu9mtyc"
StackTrace: at ASP.savecondition_aspx.__DataBinding__control7(Object sender, EventArgs e) in e:\Documents and Settings\Fernando\My Documents\Visual Studio 2008\Projects\mediTrack\mediTrack\saveCondition.aspx:line 45 at System.Web.UI.Control.OnDataBinding(EventArgs e) at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) at System.Web.UI.Control.DataBind() at System.Web.UI.Control.DataBindChildren() InnerException:

Any advice will be greatly appreciated, thank you

EDIT 2: Fixed! The problem was not in the Text or CSS tags, but in the Enabled tag, I had to cast it to a Boolean value. The problem was that the exception was signaled at the Text tag, I don't know why

like image 486
Fernando Avatar asked Nov 04 '09 18:11

Fernando


1 Answers

From the example you have given, you don't need a dataset, just the datatable. Also you are not specifying the datatype for the column.

DataTable indexTable = new DataTable();
indexTable.Columns.Add("letter", typeof(string));

//do stuff

_repeater.DataSource = indexTable;
_repeater.DataBind();

And evaluate like this

Text='<%# Eval("letter")%>'
like image 104
Mike Avatar answered Sep 28 '22 03:09

Mike