C#Repeater工作但不绑定数据

时间:2015-12-15 15:25:04

标签: c# asp.net data-binding

我有一个C#转发器正在运行对象的测试列表,它运行正确的迭代次数。问题是网页上看不到数据。我已经创建了一个对象,但是当它需要绑定它时,我似乎无法正确访问它。 我测试了对象,它们确实包含测试数据。

ASPX代码

    <asp:Repeater ID="repeater" runat="server">
    <ItemTemplate>
                <asp:Panel ID="header" runat="server">
                    <asp:Panel ID="reportName" runat="server">
                        <p class='text-bold-xlg'>
                            <asp:Label ID="CampaignNameData" Text="Campaign Name" runat="server"></asp:Label></p>
                        <p class="text-md">
                            <asp:Label ID="ReportRangeLabel" Text="Report Range: " runat="server"></asp:Label>
                            <asp:Label ID="ReportRangeData" Text="" runat="server"></asp:Label>
                        </p>
                    </asp:Panel>
                    <asp:Panel ID="logo" runat="server">
                        <img src="images/Picture1.jpg" runat="server" enableviewstate="true" />
                    </asp:Panel>
                </asp:Panel>
                <asp:Panel ID="info" runat="server">
                    <asp:Panel ID="drPersonal" runat="server">
                        <p class='text-bold-lg'>
                            <asp:Label ID="DoctorNameData" Text="<%# DataBinder.Eval(Container.DataItem, "Name") %>"></asp:Label></p>
                        <asp:Table ID='drInfoTable' runat="server">
                            <asp:TableRow>
                                <asp:TableCell>
                                    <p class='text-bold-md'>
                                        <asp:Label ID="SpecialtyLabel" Text="Specialty:" runat="server"></asp:Label></p>
                                </asp:TableCell>
                                <asp:TableCell>
                                    <p class='text-md'>
                                        <asp:Label ID="SpecialtyData" Text="<%# DataBinder.Eval(Container.DataItem, "Specialty")  %>"></asp:Label></p>
                                </asp:TableCell>
                            </asp:TableRow>
                <..close table>
      </itemTemplate>
      </asp:Repeater>

ASPX.CS代码

  protected void Page_Load(object sender, EventArgs e)
{
    List<Doctor> lstHcp = new List<Doctor>();
    Doctor a = new Doctor();
    a.Name = "Dr. A";
    a.Decile = "10";

    Doctor b = new Doctor();
    b.Name = "Dr. B";
    b.Decile = "5";

    Doctor c = new Doctor();
    c.Name = "Dr. C";
    c.Decile = "7";

    Doctor d = new Doctor();
    d.Name = "Dr. D";
    d.Decile = "2";

    lstHcp.Add(a);
    lstHcp.Add(b);
    lstHcp.Add(c);
    lstHcp.Add(d);


    repeater.DataSource = lstHcp;
    repeater.DataBind();

2 个答案:

答案 0 :(得分:1)

DataBinder.Eval(Container.DataItem, "Specialty")

将其更改为以下内容:

DataBinder.Eval(Container.DataItem, "Decile")

答案 1 :(得分:1)

asp:Label缺少runat属性。

该行应为:

<asp:Label ID="DoctorNameData" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>'></asp:Label>

(注意Text属性使用单引号。否则将抛出The server tag is not well formed.异常)

相关问题