<asp:Repeater ..>
<ItemTemplate>
<% string age = Eval("a").ToString() %>
<%
age = a.ToLower(); // real stuff here
%>
<p>Hello <%# Eval("name") %> you are <%= age %> old</p>
</ItemTemplate>
</asp:Repeater>
我收到一条错误说:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
答案 0 :(得分:1)
使用<%# Eval("<propertyName>") %>
当然,您必须为转发器分配DataSource
,然后致电DataBind()
而且,如果不使用这些内联编码,您可以将整个逻辑包装到数据项的自定义属性中。例如,与上面的代码一样,您可以创建一个自定义属性,例如Age
,如:
partial class YourDataItemClass // use partial if it is auto-generated
{
public string Age
{
var ageStr = a.ToString(); // assuming YourDataItemClass has an `a` var/property
// Do real stuff here
...
...
var lowered = ageStr.ToLower();
...
...
return lowered;
}
}
您可以在转发器控件中公开该属性,如:
<asp:Repeater id="myRepeater" ..>
<ItemTemplate>
<p>Hello <%# Eval("Name") %> you are <%# Eval("Age") %> old</p>
</ItemTemplate>
</asp:Repeater>
在代码隐藏中的某处分配数据源和数据绑定转发器,如:
...
// Call the method which provides you the data
// IEnumerable<YourDataItemClass> myData = ... ;
myRepeater.DataSource = myData;
myRepeater.DataBind();
...
答案 1 :(得分:0)
<asp:Repeater>
<ItemTemplate>
<p>Hello <%# Eval("name") %> you are <%# Convert.ToString(Eval("a")).ToLower() %> old</p>
</ItemTemplate>
</asp:Repeater>