C# ASP.NET 中继器和 DataBinder.Eval
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1675783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET Repeater and DataBinder.Eval
提问by Fernando
I've got a <asp:Repeater>
in my webpage, which is bound to a programatically created dataset.
<asp:Repeater>
我的网页中有一个,它绑定到以编程方式创建的数据集。
The purpose of this repeater is to create an index from A-Z, which, when clicked, refreshes the information on the page.
这个转发器的目的是从 AZ 创建一个索引,当点击它时,刷新页面上的信息。
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.
但是,当我运行该页面时,在Text='<%#DataBinder.Eval(Container.DataItem,"letter")'
. 我不知道为什么,我尝试使用 (String) 手动转换为 String,我尝试了 ToString() 方法,我已经尝试了所有方法。
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.
此外,如果在调试器中我为 DataBinder.Eval(Container.DataItem,"letter") 添加了一个监视,它返回的值是“A”,据我说,这对于 Text 属性应该没问题。
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:
System.InvalidCastException
未被用户代码处理Message="指定的强制转换无效。" 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( ) 内部异常:
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
编辑2: 固定!问题不在于 Text 或 CSS 标签,而是在 Enabled 标签中,我不得不将其转换为布尔值。问题是在 Text 标记处发出了异常信号,我不知道为什么
回答by roman m
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>
回答by Mike
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")%>'
回答by HectorMac
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>
回答by Gridly
It looks like the issue is with the Enabled value - it needs to be boolean.
看起来问题出在 Enabled 值上 - 它需要是布尔值。
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>