1

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;
        }
    }
  • 2
    @Dilshod, that is for winforms and would be more confusing than helpful because the differences in databinding between asp.net and winforms. – Khan Sep 27 '13 at 15:02
  • 1
    You have it almost. You need to set `rptContent`'s DataSource to your data (containing T, L, and S), then call `rptContent.DataBind()` to link it all together. – Garrison Neely Sep 27 '13 at 15:04
  • So have you made an attempt to bind the data? What problems are you having with your attempt? Is it not compiling, is it giving an error, or the wrong output or what? – Servy Sep 27 '13 at 15:04
  • 1
    @Khan my bad, I didn't look at the tag, code was very similar. Here is the new link: http://stackoverflow.com/questions/13879275/asp-net-listbox-datasource-and-databind – Dilshod Sep 27 '13 at 15:06
  • It's just not binding and I'm not sure how to do it – user2821919 Sep 27 '13 at 15:07
  • @user2821919 Currently you're not actually binding any data to your repeater. You need to do that somewhere. – Servy Sep 27 '13 at 15:11
  • @user2821919 Check this link. Repeater Databinding. http://www.ezineasp.net/post/ASP-Net-C-sharp-Repeater-Databinding-using-DataSource.aspx – Garrison Neely Sep 27 '13 at 15:16

2 Answers2

1

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;
}
Gabriel Espinoza
  • 385
  • 1
  • 18
1

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();
KodeKreachor
  • 8,852
  • 10
  • 47
  • 64
  • 1
    Why would he use DataBinder.Eval(Container.DataItem, "Fields['T'].Value") instead of just Eval("")? I have never used them on my projects. If you suggest something, you should provide information on why, don't you think? – Gabriel Espinoza Sep 27 '13 at 16:16
  • Fair enough. I've used that syntax on many projects with success, just providing an alternative. http://forums.asp.net/p/984156/1264782.aspx The main take-away from my response is that the properties within the binding expressions are not located on the objects actually being bound. – KodeKreachor Sep 27 '13 at 16:20
  • @GabrielEduardoEspinozaErice Agree with Gabriel--a more verbose syntax doesn't help any in this case when the shorter one works just the same. – Garrison Neely Sep 27 '13 at 16:34
  • thanks for the reply, didn't know that. Anyway, don't you think is a little confusing for someone who's learning how to databind? - just a constructive comment. – Gabriel Espinoza Sep 27 '13 at 16:35