user2821919
user2821919

Reputation: 11

Databinding in C#

Can someone help me databind? I'm new to .net and c# and am following tutorials that are only getting me half way there. The aspx is the following:

<asp:Repeater ID="rptContent" runat="server">
  <HeaderTemplate>
    <table>
      <thead>
        <tr>
            <th>T</th>
            <th>L</th>
            <th>S</th>
        </tr>
      </thead>
      <tbody>
  </HeaderTemplate>
  <ItemTemplate>
    <tr>
      <td><%# Eval("T") %></td>
      <td><%# Eval("L")%></td>
      <td><%# Eval("S")%></td>
    </tr>
  </ItemTemplate>
  <FooterTemplate>
    </tbody>
    </table>
  </FooterTemplate>
</asp:Repeater>

But on the back end I don't know how to actually bind the data. If there is a tutorial someone can send me to follow for this part I'd appreciate it or if you can explain that would be great.

public List<Sample> Results()
    {    
        List<Sample> List = new List<Sample>();    
        myList.Add(new Sample { Title = "Title   
1", Link = "/item.aspx?id=1", Summary = "summary     
for Item 1" });

        return List;
    }  

    public class Content
    {
        public string T
        {
            get;
            set;
        }

        public string L
        {
            get;
            set;
        }

        public string S
        {
            get;
            set;
        }
    }

Upvotes: 1

Views: 1046

Answers (2)

KodeKreachor
KodeKreachor

Reputation: 8882

The collection you assign to the data source of your repeater needs to be a collection of items containing the properties you're intending to bind to.

The individual items in your Results collection do not directly possess L, T, & S properties so in binding this collection to your repeater, the repeater cannot find those properties. In your case, you'll need to bind to a collection of Content objects:

List<Content> contentResults = new List<Content>();
contentResults.Add(new Content(){L="el", T="tee", S="es"});
rptContent.DataSource = contentResults;
rptContent.DataBind();

Upvotes: 1

Gabriel Espinoza
Gabriel Espinoza

Reputation: 413

Can you bind directly the list of Sample? or you do need to bind it to the class Content?

The important here is: in the markup, when you use Eval(""), you have to provide the exact name of the property of the object you are binding.

If you can use the list of Sample I would do the following ASPX:

<asp:Repeater ID="rptContent" runat="server">
  <HeaderTemplate>
    <table>
      <thead>
        <tr>
            <th>T</th>
            <th>L</th>
            <th>S</th>
        </tr>
      </thead>
      <tbody>
  </HeaderTemplate>
  <ItemTemplate>
    <tr>
      <td><%# Eval("Title") %></td>
      <td><%# Eval("Link")%></td>
      <td><%# Eval("Summary")%></td>
    </tr>
  </ItemTemplate>
  <FooterTemplate>
    </tbody>
    </table>
  </FooterTemplate>
</asp:Repeater>

and in Code-Behind:

protected void Page_Load(object sender, EventArgs e)
{
    rptContent.DataSource = Results();
    rptContent.DataBind();
}
public List<Sample> Results()
{    
        List<Sample> List = new List<Sample>();    
        myList.Add(new Sample { Title = "Title   
1", Link = "/item.aspx?id=1", Summary = "summary     
for Item 1" });

        return List;
}

Upvotes: 1

Related Questions