0

I am new to Repeater and DataBinding and I need help using it.

In PageLoad, I have

var photos = from p in MyDataContext.Photos
             select new {
               p,
               Url = p.GetImageUrl()
             };
repeater1.DataSource = photos;
repeater1.DataBind();

In the Repeater control, I have

<ItemTemplate>
  <% Photo p = (Photo) Eval("p"); %> <!-- Apparently I can't do this -->
  ...
  <asp:TextBox runat="server" ID="txtTime" Text='<%= p.Time == null ? "" : ((DateTime)p.Time).ToString("dd/MM/yyyy HH:mm:ss") %>' />
  ...
</ItemTemplate>

But that is wrong.

What I need is to get the Photo object in ItemTemplate so I can do things with it (eg. to display the time as in the second line in ItemTemplate above). Is it even possible to do this in a Repeater?

Could someone point me to the right direction?

Thank you in advance!

Aximili
  • 28,626
  • 56
  • 157
  • 216

3 Answers3

1

Try something like this In the onDatabound event

if (e.Item.ItemType = ListItemType.Item)
{
  photo p = (photo)e.DataItem;
  Textbox txtTime = (Textbox)e.Item.FindControl("txtTime");

  txtTime.text = (p.Time == null ? "" : ((DateTime)p.Time).ToString("dd/MM/yyyy HH:mm:ss"));
}

Edit -

Sorry, I didn't see the extra Url there. I looks like you might have to create a small class or struct.

See this Stackoverflow link for a hack workaround.

Paul Suart's post in that thread made a valid point.

Community
  • 1
  • 1
Lareau
  • 1,982
  • 1
  • 26
  • 47
  • Thanks Lareau, but e.Item.DataItem is not Photo, I don't know what type it is... something like IQueryable (see my code above, var photos = ...) – Aximili Nov 14 '10 at 04:38
0

Have you tried just:

<%# Eval("p") %>

instead of

<% Photo p = (Photo) Eval("p"); %>
Flipster
  • 4,373
  • 4
  • 28
  • 36
0

I use an alternative method. In my "Register" I import the object class:

<%@ Import Namespace="Test.Test.TO" %>

With this It's possible use your object...

Next, I created an object the same type I want to bound in my codebehind, global variable...

public Test test;

In my Repeater inside ItemTemplete:

<span style="display: none;"> <%# test = (Test)Container.DataItem %> </span>

Now, you can use all object's properties, inclusive ToString to format with culture...

Sorry for my english.