ASP.NET Repeater and DataBinder.Eval

I have a <asp:Repeater> on my web page that is bound to a software-generated dataset.

The purpose of this repeater is to create an index from AZ, which, when clicked, updates the information on the page.

The repeater has a link button like this:

 <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 data set is created as follows:

 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 launch the page, "The specified cast is not a valid exception" is thrown into Text='<%#DataBinder.Eval(Container.DataItem,"letter")' . I have no idea why, I tried manually casting to String with (String), I tried the ToString () method, I tried everything.

Also, if in the debugger I add a clock for DataBinder.Eval (Container.DataItem, "letter"), the return value is "A", which in my opinion should be good for a text property.

EDIT:

Here is the exception:

System.InvalidCastException is not handled by user code
Message = "The specified cast is invalid." Source = "App_Web_cmu9mtyc"
Stack traces: in 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 in System.Web .UI.Control.OnDataBinding (EventArgs e) in System.Web.UI.Control.DataBind (Boolean raiseOnDataBinding) in System.Web.UI.Control.DataBind () in System.Web.UI.Control.DataBindChildren () InnerException:

Any advice would be much appreciated, thanks

EDIT 2: Fixed! The problem was not in the Text or CSS tags, but in the Enabled tag I had to drop it to a boolean value. The problem was that the exception was noted in the Text tag, I donโ€™t know why

+6
c # data-binding repeater
source share
4 answers

In the example you specified, you do not need a dataset, just data. Also, you do not specify the data type for the column.

 DataTable indexTable = new DataTable(); indexTable.Columns.Add("letter", typeof(string)); //do stuff _repeater.DataSource = indexTable; _repeater.DataBind(); 

And rate it as

 Text='<%# Eval("letter")%>' 
+1
source share

I donโ€™t know if this will make any difference, but try the following (check the interval)

 <asp:LinkButton ID="indexLetter" Text='<%# Eval("letter")%>' runat="server" CssClass='<%# Eval("cssclass")%>' Enabled='<%# Eval("enabled")%>'></asp:LinkButton> 
0
source share

Do not ruin 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> 
0
source share

It seems that the problem is related to the Enabled value - it should be logical.

It 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> 
0
source share

All Articles