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 AZ, 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:
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")%>'
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>
It looks like the issue is with the Enabled value - it needs to be boolean.
This works:
<asp:LinkButton ID="indexLetter" Text='<%# this.FooData()%>'
runat="server" CssClass='<%#DataBinder.Eval(Container.DataItem, "cssclass")%>'
Enabled='<%#Convert.ToBoolean(DataBinder.Eval(Container.DataItem,"enabled"))%>'></asp:LinkButton>
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.