C# 数据表和 ASP.NET 中继器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14821858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:06:49  来源:igfitidea点击:

DataTable and ASP.NET Repeater

c#asp.netdata-bindingdatatablerepeater

提问by DMckendrick

I am binding a DataTable to a repeater. However when I run my aspx page it is not evaluating the Eval statements and displaying them on the page as

我正在将 DataTable 绑定到中继器。但是,当我运行我的 aspx 页面时,它不会评估 Eval 语句并将它们显示在页面上

<<%# Eval("Name").ToString() %> Posted by: <<%# Eval("UserName").ToString() %>
<<%# Eval("Name").ToString() %> Posted by: <<%# Eval("UserName").ToString() %>
<<%# Eval("Name").ToString() %> Posted by: <<%# Eval("UserName").ToString() %>

It is also breaking it down to 3 rows. Which is also not what I had expected. I thought it would display as spans normally would

它还将其分解为 3 行。这也不是我所期望的。我认为它会显示为跨度通常会

I have taken out the .ToString to check if it was that causing the issue. But it was not.

我已经取出 .ToString 来检查是否是导致问题的原因。但事实并非如此。

Below is my Repeater

下面是我的中继器

<asp:Repeater ID="First" runat="server">
        <ItemTemplate>
            <div class="ItemDiv">
                <span class="ItemLeft"></span>
                <span class="ItemCentre">
                    <asp:Label ID="Name" runat="server" Text='<<%# Eval("Name").ToString() %>'></asp:Label>
                </span>
                <span class="ItemRight">
                    <asp:Label ID="UserName" runat="server" Text='<<%# Eval("UserName").ToString() %>'></asp:Label>
                </span>
            </div>
        </ItemTemplate>
    </asp:Repeater>

The code to bind is the following:

绑定的代码如下:

public void dataLoad()
{
    DataTable Data = loadData(1,1,1);

    if (Data.Rows.Count < 10)
    {
        // Only populate the data from datatable
        First.DataSource = Data;
        First.DataBind();
        Second.Visible = false;
        noData.Visible = true;
    }

I have changed some of the names in the above example

我已经更改了上面示例中的一些名称

In the Debug information I can see that my DataTable is using the correct column names, and that these match my eval statements.

在调试信息中,我可以看到我的 DataTable 使用了正确的列名,并且这些与我的 eval 语句匹配。

I know I have created this in a rather strange way, from what I have read, and researched online it is more common to use a table rather than Divs/or Spans for this. So I am aware that that may be the problem at its heart.

我知道我以一种相当奇怪的方式创建了这个,从我阅读的内容和在线研究来看,为此使用表格而不是 Divs/或 Span 更为常见。所以我知道这可能是问题的核心。

I cannot see why it would be any different.

我不明白为什么会有所不同。

采纳答案by MikeSmithDev

Remove the <<from your eval statements. It should be:

<<从您的 eval 语句中删除。它应该是:

<div class="ItemDiv">
    <span class="ItemLeft"></span>
    <span class="ItemCentre">
        <asp:Label ID="Name" runat="server" Text='<%# Eval("Name").ToString() %>'></asp:Label>
    </span>
    <span class="ItemRight">
        <asp:Label ID="UserName" runat="server" Text='<%# Eval("UserName").ToString() %>'></asp:Label>
    </span>
</div>