如何访问代码后面的列表变量到aspx页面

how to access List variables in code behind to aspx page

我有一个 public class 和一个 public 列表。

public List<yahoo> yahooRec = new List<yahoo>();

public class yahoo
{

    public string url { get; set; }
    public string title { get; set; }
   public string descripton { get; set; }
}

我的列表在 foreach 循环中获取其值。我只想在转发器中使用我的列表

<asp:Repeater id="Rep" runat="server">
<ItemTemplate>
 <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%= yahoorec.url %>' Text='<%= yahoorec.title %>' />
<p> <%= yahoorec.description %> </p>
</ItemTemplate>
</asp:Repeater>

但是它work.Anybody对我没有帮助吗?

在您的 aspx.cs 页面中使用以下方法

Rep.DataSource = yahooRec;

并在您的 aspx 页面中

<asp:Repeater ID="Rep" runat="server">
<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("url") %>'></asp:Label>
    <br />
    <asp:Label ID="Label2" runat="server" Text='<%# Eval("title") %>'></asp:Label>
    <br />
    <asp:Label ID="Label3" runat="server" Text='<%# Eval("descripton") %>'></asp:Label>
</ItemTemplate>

您可以使用:

<asp:Repeater ID="Rep" runat="server">
    <ItemTemplate>
        <a href="<%# Eval("url") %>"><%# Eval("title") %></a>
        <p><%# Eval("Url")%> </p>
    </ItemTemplate>
</asp:Repeater>

或者,您可以连接到 Rep.ItemDataBound 事件,找到您的控件,并在代码隐藏中填充它们。