ASP.NET Repeater and DataBinder.Eval

Posted by Fernando on Stack Overflow See other posts from Stack Overflow or by Fernando
Published on 2009-11-04T18:36:12Z Indexed on 2012/11/11 23:01 UTC
Read the original article Hit count: 236

Filed under:
|
|
|
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET