jQuery Show Popup on Page Load

4 Jul 2012

how to bind or display images from folder to datalist using asp.net or how to insert images into folder and display images from folder to datalist using asp.net

Introduction

Here I will explain how insert images into folder and how to bind the images to datalist using asp.net.

Description:

 I will explain how to save images into folder and how we can bind that images to datalist using asp.net. It’s very easy to store images in folder and binding that images to datalist. For that first create one new website after that right click on that website select New Folder give name as Images because here I am using same name if you want to use another name you need to change the name in code also after completion of adding Imagesfolder to your website Design your aspx page like this


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Bindimagesfromfolder.aspx.cs"
    Inherits="Bindimagesfromfolder" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Bind Images to Datalist from folder</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="fileupload1" runat="server" />
        <asp:Button ID="btnsave" runat="server" Text="Upload" OnClick="btnsave_Click" />
    </div>
    <div>
        <asp:DataList ID="dtlist" runat="server" RepeatColumns="4" CellPadding="5">
            <ItemTemplate>
                <asp:Image Width="100" ID="Image1" ImageUrl='<%# Bind("Name", "~/Images/{0}") %>'
                    runat="server" />
                <br />
                <asp:HyperLink ID="HyperLink1" Text='<%# Bind("Name") %>' NavigateUrl='<%# Bind("Name", "~/Images/{0}") %>'
                    runat="server" />
            </ItemTemplate>
            <ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center"
                VerticalAlign="Bottom" />
        </asp:DataList>
    </div>
    </form>
</body>
</html>


After that add using System.IO and using System.Collections namespaces and write the following code in code behind


protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDataList();        }


    }

    protected void BindDataList()
    {
        DirectoryInfo dir = new DirectoryInfo(MapPath("Images"));
        FileInfo[] files = dir.GetFiles();
        ArrayList listItems = new ArrayList();
        foreach (FileInfo info in files)
        {
            listItems.Add(info);
        }
        dtlist.DataSource = listItems;
        dtlist.DataBind();

    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
        fileupload1.SaveAs(Server.MapPath("Images/" + filename));
        BindDataList();
    }

1 comment:

  1. This is very good and it works. I have a need for this with a twist in it. I need to make the target folder variable. So I would like to use a variable for the folder path. This is easy to do in the code behind but what to do in the aspx markup?

    I tried using "FullName" from the fileinfo object but this returns a physical path and so does not work with imagebuttons.

    Any idea how this could be done?

    ReplyDelete