C#中继器正在工作,但未绑定数据

本文关键字:绑定 数据 工作 中继器 | 更新日期: 2023-09-27 18:26:56

我有一个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();

C#中继器正在工作,但未绑定数据

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.异常)

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

将其更改为以下内容:

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