从多个文件夹中需要gridview中的图像

时间:2014-08-11 11:05:10

标签: c# asp.net datagridview aspxgridview

我有一个项目,我将图像上传到文件夹裁剪图像并保存在另一个文件夹中。 现在我想在gridview中并排显示两个图像,请参阅下面的代码并帮助我。

ASPX页面

<asp:GridView ID="gvDetails" CellPadding="5" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:ImageField DataImageUrlField="Value" ControlStyle-Height="100" ControlStyle-Width="100" HeaderText="Real File" />
        <asp:ImageField DataImageUrlField="Value" ControlStyle-Height="100" ControlStyle-Width="100" HeaderText="Crop File" />
    </Columns>

    <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>

ASPX.CS页面

String path = HttpContext.Current.Request.PhysicalApplicationPath + "images\\";
String crop_path = HttpContext.Current.Request.PhysicalApplicationPath + "images\\crop\\";
protected void Page_Load(object sender, EventArgs e) {
    string[] filesPath = Directory.GetFiles(Server.MapPath("~/images/"));
    List<ListItem> files = new List<ListItem>();
    foreach (string path in filesPath) {
        string fileName = Path.GetFileName(path);
        files.Add(new ListItem(fileName, "~/images/" + fileName));
        files.Add(new ListItem(fileName, "~/images/crop/" + fileName));
    }
    gvDetails.DataSource = files;
    gvDetails.DataBind();
}

1 个答案:

答案 0 :(得分:1)

假设两个文件夹中的图像数量相同,请尝试使用

String path = HttpContext.Current.Request.PhysicalApplicationPath + "images\\";
String crop_path = HttpContext.Current.Request.PhysicalApplicationPath + "images\\crop\\";

创建一个新类

Class filenames
{
    public string filename { get; set; }
    public string crop_filename { get; set; }
}

然后在页面加载

protected void Page_Load(object sender, EventArgs e)
{
    string[] filesPath = Directory.GetFiles(Server.MapPath("~/images/"));
    string[] crop_filesPath = Directory.GetFiles(Server.MapPath("~/images/crop/"));
    List<filenames> files = new List<filenames>();
    filenames objfilenames;
    for(int i=0; i<filesPath.length; i++)
    {
        objfilenames = new filenames();
        objfilenames.filename = "~/images/" +  Path.GetFileName(filesPath[i]);
        objfilenames.crop_filename = "~/images/crop/" + Path.GetFileName(crop_filesPath[i]);
        files.Add(objfilenames);
    }
    gvDetails.DataSource = files;
    gvDetails.DataBind();
}