0

我正在使用 c# 和 asp.net,我需要它:如何显示存储在本地驱动器中的图像?

但是我需要同时处理多个图像,如何?

4

2 回答 2

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

这里取

于 2013-02-06T04:22:34.213 回答
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>

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

检查这个链接

于 2013-02-06T04:15:31.107 回答