TO BIND IMAGES IN REPEATER USING ASP.NET

sandy taylor

Total Post:46

Points:324
Posted by  sandy taylor
C#  ASP.Net 
 989  View(s)
Ratings:
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>

  1. Andrew Watson

    Post:39

    Points:273
    Re: To bind images in repeater using asp.net

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

        }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!