user2042023
user2042023

Reputation: 153

How to display multiple images which is stored in local drive?

I'm using c# and asp.net, I need exactly it: How to display image which is stored in local drive?

But I need to do it for mutiple images at the same time, how?

Upvotes: 0

Views: 2815

Answers (2)

coder
coder

Reputation: 13248

Try this:

HTML:

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

C#:

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

Took from here

Upvotes: 1

rahul.deshmukh
rahul.deshmukh

Reputation: 590

you can display multiple images at same time using datalist control as follows:

 <asp:DataList ID="DataList1" runat="server" RepeatColumns = "2"  RepeatLayout = "Table"  Width = "500px">
 <ItemTemplate>
    <br />
      <table cellpadding = "5px" cellspacing = "0" class="dlTable">
       <tr>
          <td>
            <asp:Image ID="Image1" runat="server" ImageUrl = '<%# Eval("FilePath")%>'
             Width = "200px" Height = "200px"/>
          </td>
      </tr>
     </table>
       <br />
   </ItemTemplate>
   </asp:DataList>

binds data table or data set to the data list control you can also refer following link and do not forgot to accept if this solution works

check this link

Upvotes: 0

Related Questions