如何只显示文件夹中的特定图像到ASP.NET直放站 [英] How to only display certain images in a folder into a Repeater in ASP.NET

查看:201
本文介绍了如何只显示文件夹中的特定图像到ASP.NET直放站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中继器,需要一个文件夹中的所有我的图片并显示它。但C $ C钮,我必须做什么$只允许可以说Image1.jpg和Image2.jpg在我的中继器显示。我不不想转发到我的文件夹中显示的所有图像。

我的中继器

 < ASP:直放站ID =repImages=服务器OnItemDataBound =repImages_ItemDataBound>
&所述; HeaderTemplate中>&所述p为H.;&下; / HeaderTemplate中>
<&ItemTemplate中GT;
    < ASP:超链接ID =hlWhat=服务器的rel =imagebox-BW>
    < ASP:图片ID =imgTheImage=服务器/>
    < / ASP:超链接>
< / ItemTemplate中>
&所述; FooterTemplate>&下; / P>&下; / FooterTemplate>
< / ASP:直放站>

我的背后code - 网页加载

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(!的IsPostBack)
        {
            字符串sBasePath = System.Web.HttpContext.Current.Request.ServerVariables [APPL_PHYSICAL_PATH];
            如果(sBasePath.EndsWith(\\\\))
                sBasePath = sBasePath.Substring(0,sBasePath.Length-1);            sBasePath = sBasePath +\\\\+图片;            System.Collections.Generic.List<串GT; oList =新System.Collections.Generic.List<串GT;();
            的foreach(字符串s在System.IO.Directory.GetFiles(sBasePath,*。*))
            {
                //我们可以做,例如一些过滤只增加.jpg或东西
                oList.Add(System.IO.Path.GetFileName(S));            }
            repImages.DataSource = oList;
            repImages.DataBind();
        }    }

我的背后code - Repeater的ItemDataBound事件code

 保护无效repImages_ItemDataBound(对象发件人,RepeaterItemEventArgs E)
    {
        如果(e.Item.ItemType == || ListItemType.AlternatingItem
            e.Item.ItemType == ListItemType.Item)
        {
            字符串sFile = e.Item.DataItem为字符串;            //创建thumblink
            超链接hlWhat = e.Item.FindControl(hlWhat)作为超链接;
            hlWhat.NavigateUrl = RESOLVEURL(〜/图片/+ sFile);
            hlWhat.ToolTip = System.IO.Path.GetFileNameWithoutExtension(sFile);
            hlWhat.Attributes [相对] =imagebox-BW            图片oImg = e.Item.FindControl(imgTheImage)的图像;
            oImg.ImageUrl = RESOLVEURL(〜/ createthumb.ashx区= /图片/?+ sFile +&放大器; XMAX = 100安培; YMAX = 100);
        }    }

答:

我的更新页面加载

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(!的IsPostBack)
        {
            字符串sBasePath = System.Web.HttpContext.Current.Request.ServerVariables [APPL_PHYSICAL_PATH];
            如果(sBasePath.EndsWith(\\\\))
                sBasePath = sBasePath.Substring(0,sBasePath.Length-1);            sBasePath = sBasePath +\\\\+图片;            System.Collections.Generic.List<串GT; oList =新System.Collections.Generic.List<串GT;();            字符串[] =扩展{* .JPG,*巴纽};            清单<串GT;文件=新的List<串GT;();            的foreach(在扩展字符串过滤器)
            {
                files.AddRange(System.IO.Directory.GetFiles(sBasePath,过滤器));
                oList.Add(System.IO.Path.GetFileName(过滤器));
            }
            repImages.DataSource = oList;
            repImages.DataBind();
        }


解决方案

什么格式,你要显示的图像的名字呢?如果你知道你可以构造一个过滤器列出目录的内容时使用:

 的String []文件= Directory.GetFiles(文件夹,* 1.JPG);

将列出在1

结束所有JPG文件

编辑:

而不是具有:

 的foreach(字符串s在System.IO.Directory.GetFiles(sBasePath,*。*))
{
    //我们可以做,例如一些过滤只增加.jpg或东西
    oList.Add(System.IO.Path.GetFileName(S));
}

你必须:

 的String []文件= System.IO.Directory.GetFiles(sBasePath,* .JPG)
的foreach(在文件中字符串s)
{
    oList.Add(System.IO.Path.GetFileName(S));
}

编辑2:

我已经做了快速搜索,它看起来像获取文件不会采取多个扩展名,所以你必须寻找各类型的扩展分开:

 的String [] =扩展{* .JPG,*巴纽};清单<串GT;文件=新的List<串GT;();
的foreach(在扩展字符串过滤器)
{
    files.AddRange(System.IO.Directory.GetFiles(路径,过滤器));
}
的foreach(在文件中字符串s)
{
    oList.Add(System.IO.Path.GetFileName(S));
}

I have a Repeater that takes all my images in a folder and display it. But what code changes must I make to only allow lets say Image1.jpg and Image2.jpg to be displayed in my repeater. I don"t want the repeater to display ALL the images in my folder.

My Repeater

<asp:Repeater ID="repImages" runat="server" OnItemDataBound="repImages_ItemDataBound">
<HeaderTemplate><p></HeaderTemplate>
<ItemTemplate>
    <asp:HyperLink ID="hlWhat" runat="server" rel="imagebox-bw">
    <asp:Image ID="imgTheImage" runat="server" />
    </asp:HyperLink>
</ItemTemplate>
<FooterTemplate></p></FooterTemplate>
</asp:Repeater>

My Code behind - PAGE LOAD

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string sBasePath = System.Web.HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];
            if ( sBasePath.EndsWith("\\"))
                sBasePath = sBasePath.Substring(0,sBasePath.Length-1);

            sBasePath = sBasePath + "\\" + "pics";

            System.Collections.Generic.List<string> oList = new System.Collections.Generic.List<string>();
            foreach (string s in System.IO.Directory.GetFiles(sBasePath, "*.*"))
            {
                //We could do some filtering for example only adding .jpg or something
                oList.Add( System.IO.Path.GetFileName( s ));

            }
            repImages.DataSource = oList;
            repImages.DataBind();
        }

    }

My Code behind - Repeater's ItemDataBound event code

protected void repImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            string sFile = e.Item.DataItem as string;

            //Create the thumblink
            HyperLink hlWhat = e.Item.FindControl("hlWhat") as HyperLink;
            hlWhat.NavigateUrl = ResolveUrl("~/pics/" + sFile  );
            hlWhat.ToolTip = System.IO.Path.GetFileNameWithoutExtension(sFile);
            hlWhat.Attributes["rel"] = "imagebox-bw";

            Image oImg = e.Item.FindControl("imgTheImage") as Image;
            oImg.ImageUrl = ResolveUrl("~/createthumb.ashx?gu=/pics/" + sFile + "&xmax=100&ymax=100" );


        }

    }

ANSWER:

My updated Page Load

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string sBasePath = System.Web.HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];
            if ( sBasePath.EndsWith("\\"))
                sBasePath = sBasePath.Substring(0,sBasePath.Length-1);

            sBasePath = sBasePath + "\\" + "pics";

            System.Collections.Generic.List<string> oList = new System.Collections.Generic.List<string>();

            string[] extensions = { "*.jpg", "*.png" };

            List<string> files = new List<string>(); 

            foreach (string filter in extensions) 
            {
                files.AddRange(System.IO.Directory.GetFiles(sBasePath, filter)); 
                oList.Add(System.IO.Path.GetFileName(filter));
            }


            repImages.DataSource = oList;
            repImages.DataBind();
        }

解决方案

What format are the image names that you want to display? If you know that you can construct a filter to use when listing the contents of the directory:

string[] files = Directory.GetFiles(folder, "*1.jpg");

Will list all the jpg files that end in "1"

EDIT:

Instead of having:

foreach (string s in System.IO.Directory.GetFiles(sBasePath, "*.*"))
{
    //We could do some filtering for example only adding .jpg or something
    oList.Add( System.IO.Path.GetFileName( s ));
}

You'd have:

string[] files = System.IO.Directory.GetFiles(sBasePath, "*.jpg")
foreach (string s in files)
{
    oList.Add( System.IO.Path.GetFileName( s ));
}

EDIT 2:

I've done a quick search and it looks like Get Files won't take multiple extensions, so you'll have to search for each type of extension separately:

string[] extensions = {"*.jpg" , "*.png" };

List<string> files = new List<string>();
foreach(string filter in extensions)
{
    files.AddRange(System.IO.Directory.GetFiles(path, filter));
}
foreach (string s in files)
{
    oList.Add( System.IO.Path.GetFileName( s ));
}

这篇关于如何只显示文件夹中的特定图像到ASP.NET直放站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆