开发者

ASP.NET - Getting the object inside Repeater ItemTemplate with/without Eval

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()
             };
repeat开发者_如何转开发er1.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!


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.


Have you tried just:

<%# Eval("p") %>

instead of

<% Photo p = (Photo) Eval("p"); %>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜