本文介绍了如何将不同文件夹中的图像显示为datalist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个图像文件夹,其中我有3个不同。我想将这三个文件夹中的图像转换为datalist我该怎么做。 目前我只使用以下代码显示图像文件夹中的图像: < asp:DataList ID = dlImages runat = server RepeatColumns = 3 CellPadding = 3 style = margin-left:0px > < ItemTemplate> < a id = imageLink href = ' <%#Eval(ImageName,〜/ Images / {0})%>' title = ' <%#Eval(Description)%>' rel = lightbox [Brussels] runat = 服务器 > < asp:图片ID = Image1 ImageUrl = ' <%#Bind( ImageName,〜/ Images / {0})%>' runat = 服务器宽度= 100高度= 100 /> < / a > < / ItemTemplate > < ItemStyle BorderColor = Brown BorderStyle = dotted BorderWidth = 3px HorizontalAlign = 中心 VerticalAlign = 底部 /> < / asp:DataList > 现在我想在三个不同的文件夹中显示该数据库中的图像我该怎么办呢 提前致谢解决方案 < asp:datalist id = dlImages runat = server repeatcolumns = 3 cellpadding = 3 xmlns:asp = #unknown > Style =margin-left:0px onitemdatabound =dlImages_ItemDataBound> < itemtemplate > < a id = imageLink > rel =lightbox [Brussels]runat =server> < img id = Image1 runat = server / > < / a > < / itemtemplate > < item style bordercolor = Brown border style = dotted borderwidth = 3px horizontalalign = 中心 > VerticalAlign =Bottom/> < / item style > < / asp:datalist > public partial class _Default:System.Web.UI.Page { DataTable resultTable; protected void Page_Load(object sender,EventArgs e) { //虚拟数据 resultTable = new DataTable(); resultTable.Columns.Add(ImageName); resultTable.Columns.Add(Description); resultTable.Columns.Add(ID); DataRow dr = resultTable.NewRow(); dr [ID] = 1; dr [Description] =描述1; dr [ImageName] =Sharepoint-2010-powered-by-itgroove.jpg; resultTable.Rows.Add(dr); dr = resultTable.NewRow(); dr [ID] = 2; dr [Description] =描述2; dr [ImageName] =nature_6.jpg; resultTable.Rows.Add(dr); dr = resultTable.NewRow(); dr [ID] = 3; dr [Description] =描述3; dr [ImageName] =werkzoekende.png; resultTable.Rows.Add(dr); dlImages.DataSource = resultTable; dlImages.DataBind(); } protected void dlImages_ItemDataBound(object sender,DataListItemEventArgs e) { string filepath = GetImagePath(Convert.ToString(resultTable) .Rows [e.Item.ItemIndex] [ ImageName])); // set anchor tag Object objImageLink = e.Item.FindControl(imageLink); if(objImageLink!= null) { HtmlAnchor anchor = objImageLink as HtmlAnchor; anchor.HRef = filepath; anchor.DataBind(); } //设置图像控制 Object objImage = e.Item.FindControl(Image1); if(objImage!= null) { HtmlImage img = objImage as HtmlImage; img.Src = filepath; img.Width = 100; img.Height = 100; img.DataBind(); } } //获取图像路径 public string GetImagePath(string imgName) { string imgFolderPath = Server.MapPath ( 〜/图像); string [] seprator = {Images}; string filepath = string.Empty; var file = Directory.GetFiles(imgFolderPath,imgName,SearchOption.AllDirectories).FirstOrDefault(); if(file == null) { //处理未找到的文件} else { filepath = 〜/ Images+ file.Split(seprator,StringSplitOptions.None)[1]; filepath = filepath.Replace(\\,/); } 返回文件路径; } } I have an images folder in which i have 3 different. I want to images from those three folder into datalist how can i do that.At present i am showing images present only in image folder using this code:<asp:DataList ID="dlImages" runat="server" RepeatColumns="3" CellPadding="3" style="margin-left: 0px"><ItemTemplate><a id="imageLink" href='<%# Eval("ImageName","~/Images/{0}") %>' title='<%#Eval("Description") %>' rel="lightbox[Brussels]" runat="server" ><asp:Image ID="Image1" ImageUrl='<%# Bind("ImageName", "~/Images/{0}") %>' runat="server" Width="100" Height="100" /></a></ItemTemplate><ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center" VerticalAlign="Bottom" /></asp:DataList>Now i want to show images in that datalist from three different folder how can i do thatThanks in advance 解决方案<asp:datalist id="dlImages" runat="server" repeatcolumns="3" cellpadding="3" xmlns:asp="#unknown"> Style="margin-left: 0px" onitemdatabound="dlImages_ItemDataBound"> <itemtemplate> <a id="imageLink"> rel="lightbox[Brussels]" runat="server"> <img id="Image1" runat="server" /> </a> </itemtemplate> <itemstyle bordercolor="Brown" borderstyle="dotted" borderwidth="3px" horizontalalign="Center"> VerticalAlign="Bottom" /> </itemstyle></asp:datalist>public partial class _Default : System.Web.UI.Page { DataTable resultTable; protected void Page_Load(object sender, EventArgs e) { // Dummy data resultTable = new DataTable(); resultTable.Columns.Add("ImageName"); resultTable.Columns.Add("Description"); resultTable.Columns.Add("ID"); DataRow dr = resultTable.NewRow(); dr["ID"] = 1; dr["Description"] = "Description 1"; dr["ImageName"] = "Sharepoint-2010-powered-by-itgroove.jpg"; resultTable.Rows.Add(dr); dr = resultTable.NewRow(); dr["ID"] = 2; dr["Description"] = "Description 2"; dr["ImageName"] = "nature_6.jpg"; resultTable.Rows.Add(dr); dr = resultTable.NewRow(); dr["ID"] = 3; dr["Description"] = "Description 3"; dr["ImageName"] = "werkzoekende.png"; resultTable.Rows.Add(dr); dlImages.DataSource = resultTable; dlImages.DataBind(); } protected void dlImages_ItemDataBound(object sender, DataListItemEventArgs e) { string filepath = GetImagePath(Convert.ToString(resultTable.Rows[e.Item.ItemIndex]["ImageName"])); // set anchor tag Object objImageLink = e.Item.FindControl("imageLink"); if (objImageLink != null) { HtmlAnchor anchor = objImageLink as HtmlAnchor; anchor.HRef = filepath; anchor.DataBind(); } // set image control Object objImage = e.Item.FindControl("Image1"); if (objImage != null) { HtmlImage img = objImage as HtmlImage; img.Src = filepath; img.Width = 100; img.Height = 100; img.DataBind(); } } // To get image path public string GetImagePath(string imgName) { string imgFolderPath = Server.MapPath("~/Images"); string[] seprator = {"Images"}; string filepath = string.Empty; var file = Directory.GetFiles(imgFolderPath, imgName, SearchOption.AllDirectories).FirstOrDefault(); if (file == null) { // Handle the file not being found } else { filepath = "~/Images" + file.Split(seprator, StringSplitOptions.None)[1]; filepath = filepath.Replace("\\", "/"); } return filepath; } } 这篇关于如何将不同文件夹中的图像显示为datalist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 21:09