0

This is my code:

Codebehind:

public class data
....
...

public List<dataImages> Images { get; set; }
...

var data= GarageBLL.LoadData(Convert.ToInt32(DataId), Convert.ToInt32(MemberId));
rptImages.DataSource = data.Images.Take(3);
rptImages.DataBind();

aspx:

   <asp:Repeater runat="server" ID="rptImages">
        <ItemTemplate>
            <asp:Image runat="server" CssClass="img1" ImageUrl='<%# String.Format("/images/{0}/{1}.{2}", DataId, Eval("ImageId"), Eval("Extension")) %>' />
        </ItemTemplate>
    </asp:Repeater>

So, this works at the moment, but i would like to add different classes to each image. Anyone got any idea how i can do that?

4

1 回答 1

2

您可以使用中继器的ItemDataBound事件来设置CssClass

protected void rptImages_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
      // This event is raised for the header, the footer, separators, and items.
      // Execute the following logic for Items and Alternating Items.
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
         dataImages img = (dataImages) e.Item.DataItem;
         Image img1 = (Image) e.Item.FindControl("img1");
         // add your correct logic here according to the dataImages properties
         img.CssClass = "YourCssClass";
         // assuming you just want different classes for your three images, use ItemIndex with remainder:
         string class = "img" + (e.Item.ItemIndex % 3 + 1).ToString() + "class";
         img.CssClass = class;
      }
   }    
于 2012-11-03T17:26:23.213 回答