views:

1245

answers:

3

Hello, 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:

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

A: 

I don't know if this will make any difference, but try for following (note spacing too)

<asp:LinkButton ID="indexLetter" Text='<%# Eval("letter")%>'
runat="server"   CssClass='<%# Eval("cssclass")%>'
Enabled='<%# Eval("enabled")%>'></asp:LinkButton>
roman m
it also didn't work... I really don't know what is going on!
Fernando
A: 

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")%>'
Mike
unfortunately, it still happens if I do it this way (adding the datatype to the column, and only using a dataTable)I edited the post to leave the exception trace
Fernando
A: 

Not to spoil the exercise, but what's wrong with hard coding:

<a href="Page.aspx?LIndex=A">A</a>
<a href="Page.aspx?LIndex=B">B</a>
<a href="Page.aspx?LIndex=C">C</a>
...
<a href="Page.aspx?LIndex=Z">Z</a>
HectorMac