0
<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.
4

2 回答 2

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();
...
于 2012-12-11T01:28:15.840 回答
0
<asp:Repeater>
<ItemTemplate>
 <p>Hello <%# Eval("name") %> you are <%# Convert.ToString(Eval("a")).ToLower()  %>   old</p>

</ItemTemplate>    
</asp:Repeater>
于 2012-12-11T11:52:55.423 回答