coding
coding

Reputation: 71

C# Repeater working but is not binding data

I have a C# repeater that is running a test list of objects and it runs the correct amount of iterations. The problem is is that the data is not visible on the web page. I have created an object but it seems like I am not accessing it correctly when it is time to bind it. I have tested the objects and they do contain the test data.

ASPX CODE

    <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 CODE

  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();

Upvotes: 0

Views: 1157

Answers (2)

Frank Witte
Frank Witte

Reputation: 466

The asp:Label is missing the runat attribute.

The line should be:

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

(notice the use of single quotes for the Text attribute. Else a The server tag is not well formed. exception will be thrown)

Upvotes: 1

Khazratbek
Khazratbek

Reputation: 1656

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

change it to following:

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

Upvotes: 1

Related Questions