列表视图项目数据插入列表<>在c#asp.net中

时间:2012-09-27 05:22:08

标签: c# asp.net

我的列表视图ItemTemplate如下

<a href='<%# getpath(Eval("IMAGE_PATH")) %>' title='<%# Eval("IMAGE_DESCRIPTION") %>'>
   <asp:Image ID="capty" CssClass="capty" runat="server" 
       AlternateText='<%# Eval("IMAGE_DESCRIPTION") %>' 
       ImageUrl='<%# retriveurl(Eval("IMAGE_PATH")) %>' 
       Height="100px" Width="150px">
   </asp:Image>
</a>

我的代码隐藏源是

int count=ListView1.Items.Count;
    List<ImageGallery> _list = new List<ImageGallery>();
    for (int i = 0; i < count; i++)
    {
        ImageGallery ob = new ImageGallery();
        ob.ImageName = ??
        ob.Description = ??
        _list.Add(ob);
    }

现在我想将Eval(“IMAGE_DESCRIPTION”)数据插入到ob.Description和ob.ImageName = Eval(“IMAGE_ID”)。如何做?

List<ImageGallery> _list = new List<ImageGallery>(); 
for (int i = 0; i < ListView1.Items.Count; i++) 
{ 
    ImageGallery ob = new ImageGallery(); 
    ob.ImageName = ""; 
    ob.Description = " "; 
    _list.Add(ob); 
}

1 个答案:

答案 0 :(得分:0)

您可以使用FindControl来获取图片。你传递了你想要找到的控件的ID(“capty”);它返回一个对象,因此您需要将其转换/取消装入控件类型Image。见下文:

List<ImageGallery> _list = new List<ImageGallery>(); 
for (int i = 0; i < ListView1.Items.Count; i++) 
{
    Image image = (Image)ListView1.Items[i].FindControl("capty");
    ImageGallery ob = new ImageGallery(); 
    ob.ImageName = ""; 
    ob.Description = image.AlternateText; 
    _list.Add(ob); 
}