使用阵列中的中继器

发布于 2024-12-27 08:57:39 字数 517 浏览 0 评论 0原文

假设我有一组逗号分隔的列表,需要将其显示到 Repeater 中,我该怎么做?

那么,

List1 = "1, 2, 3, 4"
List2 = "a, b, c, d"

如果我将它们转换为数组,如何将它们绑定到Repeater并获取Repeater中每个列表的值?

我想要获得如下所示的 HTML 输出:

<div>
    <span> CONTENT TO LIST 1(0) </span>
    <span> CONTENT OF LIST 2(0) </span>
</div>

<div>
    <span> CONTENT TO LIST 1(1) </span>
    <span> CONTENT OF LIST 2(1) </span>
</div>
.... etc

Supposing I have a set of comma separated lists which I need to display into a Repeater, how would I?

So,

List1 = "1, 2, 3, 4"
List2 = "a, b, c, d"

If I convert these to arrays, how do I bind them to a Repeater and get the values of each list within the Repeater?

I want to get an HTML output that looks like this:

<div>
    <span> CONTENT TO LIST 1(0) </span>
    <span> CONTENT OF LIST 2(0) </span>
</div>

<div>
    <span> CONTENT TO LIST 1(1) </span>
    <span> CONTENT OF LIST 2(1) </span>
</div>
.... etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无妨# 2025-01-03 08:57:39

是的,你可以,这是一个简单的例子。

标记:

<asp:Repeater ID="rpTest" runat="server">
    <ItemTemplate>
        <div id="row<%# Eval("ID").ToString() %>">DIV CONTENT</div>
    </ItemTemplate>
</asp:Repeater>

准备测试代码:

public class TestObject
{
    public int ID { get; set; }
    public string Text { get; set; }
}

在 Page_Load 中或初始化 Repeater 的位置:

List<TestObject> lst = new List<TestObject>();
lst.Add(new TestObject() { ID = 0, Text = "Zero" });
lst.Add(new TestObject() { ID = 1, Text = "One" });
lst.Add(new TestObject() { ID = 2, Text = "Two" });
lst.Add(new TestObject() { ID = 3, Text = "Three" });

rpTest.DataSource = lst;
rpTest.DataBind();

输出:

<div id="row0">DIV CONTENT</div>     
<div id="row1">DIV CONTENT</div>     
<div id="row2">DIV CONTENT</div>    
<div id="row3">DIV CONTENT</div>

编辑:

要绑定到 Array,只需分配 < code>Array 到 DataSource,然后使用以下方式访问绑定中的 Array 项:

<%# Container.DataItem %>

Yes you can, here is a simple example.

Markup:

<asp:Repeater ID="rpTest" runat="server">
    <ItemTemplate>
        <div id="row<%# Eval("ID").ToString() %>">DIV CONTENT</div>
    </ItemTemplate>
</asp:Repeater>

Prep Test Code:

public class TestObject
{
    public int ID { get; set; }
    public string Text { get; set; }
}

In your Page_Load or where you initialize the Repeater:

List<TestObject> lst = new List<TestObject>();
lst.Add(new TestObject() { ID = 0, Text = "Zero" });
lst.Add(new TestObject() { ID = 1, Text = "One" });
lst.Add(new TestObject() { ID = 2, Text = "Two" });
lst.Add(new TestObject() { ID = 3, Text = "Three" });

rpTest.DataSource = lst;
rpTest.DataBind();

Output:

<div id="row0">DIV CONTENT</div>     
<div id="row1">DIV CONTENT</div>     
<div id="row2">DIV CONTENT</div>    
<div id="row3">DIV CONTENT</div>

EDIT:

To bind to an Array just assign the Array to the DataSource and then access the Array items in your binding with:

<%# Container.DataItem %>
老子叫无熙 2025-01-03 08:57:39

在 .net webforms 中,您可以使用

<% for (int i = 0; i < totalRequired; i++)
   { %>
   <div id="<%= i %>"></div>
<% } %>

希望有帮助。

in .net webforms you could use

<% for (int i = 0; i < totalRequired; i++)
   { %>
   <div id="<%= i %>"></div>
<% } %>

Hope that helps.

万劫不复 2025-01-03 08:57:39

我有一个建议aspx:

<asp:Repeater ID="rep" runat="server">
        <ItemTemplate>
            <div>
                <span>CONTENT TO LIST 1(<%# DataBinder.Eval(Container, "DataItem.Key").ToString()%>)</span>
                <span>CONTENT TO LIST 2(<%# DataBinder.Eval(Container, "DataItem.Value").ToString()%>)</span>
            </div>
        </ItemTemplate>
    </asp:Repeater>

背后的代码:

Dim test() As String = "1,2,3,4".Split(",")
Dim test2() As String = "a,b,c,d".Split(",")

Dim ls As New List(Of KeyValuePair(Of String, String))
For i As Integer = 0 To test.Count - 1
    ls.Add(New KeyValuePair(Of String, String)(test(i), test2(i)))
Next
rep.DataSource = ls
rep.DataBind()

I have a suggestion the aspx:

<asp:Repeater ID="rep" runat="server">
        <ItemTemplate>
            <div>
                <span>CONTENT TO LIST 1(<%# DataBinder.Eval(Container, "DataItem.Key").ToString()%>)</span>
                <span>CONTENT TO LIST 2(<%# DataBinder.Eval(Container, "DataItem.Value").ToString()%>)</span>
            </div>
        </ItemTemplate>
    </asp:Repeater>

The code behind:

Dim test() As String = "1,2,3,4".Split(",")
Dim test2() As String = "a,b,c,d".Split(",")

Dim ls As New List(Of KeyValuePair(Of String, String))
For i As Integer = 0 To test.Count - 1
    ls.Add(New KeyValuePair(Of String, String)(test(i), test2(i)))
Next
rep.DataSource = ls
rep.DataBind()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文