Home > DeveloperSection > Forums > To bind images in repeater using asp.net
sandy taylor
sandy taylor

Total Post:46

Points:324
Posted on    June-01-2015 12:50 AM

 .NET C#  ASP.Net 
Ratings:


 1 Reply(s)
 540  View(s)
Rate this:
in this sample i want to bind the image from local drive to repeater but not bind properly.


string[] filePaths = Directory.GetFiles("D:\\Users\\Images\\Pictures\\");

            List<ListItem> files = new List<ListItem>();

            foreach (string filePath in filePaths)

            {

                string fileName = Path.GetFileName(filePath);

                files.Add(new ListItem(fileName, @"D:\Users\Images\Pictures\" + fileName)); //i can get image name  --D:\Users\Images\Pictures\flight.jpg

            }

            Repeater1.DataSource = files;

            Repeater1.DataBind();

 <asp:Repeater ID="Repeater1" runat="server">

                        <ItemTemplate>

                            <li>

                                <img src='<%# DataBinder.Eval(Container.DataItem,"Value") %>' title='<%# (DataBinder.Eval(Container.DataItem,"Text").ToString()).Split('.')[0].ToString() %>' alt=""></li>

                        </ItemTemplate>

                    </asp:Repeater>



Andrew Watson
Andrew Watson

Total Post:39

Points:273
Posted on    June-01-2015 1:11 AM

Create a GenericHandler.

Note: When your application moves to production, You need to make sure D:\Users\Images folder exists in web server.

public class ImageHandler : IHttpHandler

{

    public void ProcessRequest(HttpContext context)

    {

        string fileName = context.Request.QueryString["filename"];

        context.Response.ContentType = "image/jpeg";

        context.Response.TransmitFile(@"D:\Users\Images\Pictures\" + fileName);

    }

 

    public bool IsReusable

    {

        get { return false; }

    }

}

 

ASPX

<asp:Repeater ID="Repeater1" runat="server">

    <ItemTemplate>

        <asp:Image runat="server"

            ImageUrl='<%# "~/ImageHandler.ashx?filename=" + Eval("Value") %>' />

    </ItemTemplate>

</asp:Repeater>

 

protected void Page_Load(object sender, EventArgs e)

{

    if (!IsPostBack)

    {

        string[] filePaths = Directory.GetFiles(@"D:\Users\Images\Pictures");

        List<ListItem> files = new List<ListItem>();

        foreach (string filePath in filePaths)

        {

            string fileName = Path.GetFileName(filePath);

            files.Add(new ListItem(fileName, fileName));

        }

        Repeater1.DataSource = files;

        Repeater1.DataBind();

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick