简体   繁体   中英

List view Item data insert to a List<> in c# asp.net

My list view ItemTemplate is as follows

<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>

my codebehind source is

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);
    }

now i want to insert the Eval("IMAGE_DESCRIPTION") data to a ob.Description and ob.ImageName=Eval("IMAGE_ID") .How to do this?

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); 
}

You can use FindControl to get the image. You pass it the ID of the control you want to find ("capty"); it returns an object, so you need to cast/unbox it to the control type Image . See below:

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); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM