如何显示存储在本地驱动器中的多个图像?

时间:2013-02-06 04:03:45

标签: c# asp.net

我正在使用c#和asp.net,我需要它:How to display image which is stored in local drive?

但是我需要同时为多张图片做这件事,怎么样?

2 个答案:

答案 0 :(得分:1)

试试这个:

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

here

开始

答案 1 :(得分:0)

您可以使用datalist控件同时显示多个图像,如下所示:

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

将数据表或数据集绑定到数据列表控件,您也可以参考以下链接,如果此解决方案有效,请不要忘记接受

check this link