【问题标题】:DataRepeater without subcontrols没有子控件的 DataRepeater
【发布时间】:2009-11-11 20:18:58
【问题描述】:

在 datarepeater 控件中执行此操作的正确方法是什么?

                <asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                <strong><%= Eval("FullName") %></strong><br />
                <p>
                <%= Eval("Summary") %>
                </p>
                </ItemTemplate>
                </asp:Repeater>

遇到错误数据绑定方法如 Eval()、XPath() 和 Bind() 只能在数据绑定控件的上下文中使用。

我只想写出全名和摘要。但我不想嵌套子控件。 Repsonse.Write 是最好的方法吗?

更新: 不确定这是否有必要,但我能够解决它的唯一方法是使用控件

【问题讨论】:

  • 您的代码中是否缺少Repeater1.DataSource 和Repeater1.DataBind()?
  • 不。 CodeBehind 没有任何问题。

标签: c# .net asp.net data-binding


【解决方案1】:

中继器需要一个数据源,分配如下:

public class Foo
{
public string FullName { get;set; } 
public string Summary {get;set; }

public Foo(fullName,summary)
{
  FullName=fullName;
  Summary=summary;
}
}

/// elsewhere...
List<Foo> myFoos = new List<Foo>();
myFoos.Add(new Foo("Alice","Some chick"));
myFoos.Add(new Foo("Bob","Some guy"));
myFoos.Add(new Foo("Charlie","Indeterminate"));
Repeater1.DataSource = myFoos;
Repeater1.DataBind();

正如本示例所示,您的数据源可以是任何实现 IEnumerable 的东西 - 列表是我的最爱,但 C# 中的大多数集合都属于这一类。您的数据源不一定必须来自数据库或任何特定的地方。

您不必使用 response.write 或子控件。 (无论如何,服务器控件在中继器内无效)。您可以尝试替换

<%=Eval("...

<%#Eval("...

我不确定其中的区别,但大多数示例都使用第二种形式。

【讨论】:

  • with
【解决方案2】:

您可以随时尝试以下操作:

 <%# DataBinder.Eval(Container.DataItem, "FullName") %>

【讨论】:

  • 这给了我一个错误。不能在 DataBound 控件之外使用 DataBinder?
  • 您没有将中继器绑定到某个数据源吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多