如何在 ASP.NET 中仅将文件夹中的某些图像显示到转发器中

发布于 2024-07-16 02:07:30 字数 3300 浏览 4 评论 0原文

我有一个中继器,可以将所有图像放入文件夹中并显示它。 但是我必须对代码进行哪些更改才能只允许在我的中继器中显示 Image1.jpg 和 Image2.jpg 。 我不希望转发器显示文件夹中的所有图像。

我的转发器

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

后面的代码 - 页面加载

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

    }

我后面的代码 - 转发器的 ItemDataBound 事件代码

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" );


        }

    }

答案:

我更新的页面加载

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烈酒灼喉 2024-07-23 02:07:30

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

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

将列出以“1”结尾的所有 jpg 文件

编辑:

而不是:

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

您将拥有:

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

编辑2:

我已经完成了快速搜索,看起来“获取文件”不会采用多个扩展名,因此您必须分别搜索每种类型的扩展名:

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

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 ));
}
沉睡月亮 2024-07-23 02:07:30

最简单的方法是将它们全部加载到 List<> 中 然后使用Linq过滤出你想要的。

VS2005

public class GetFiles
{

    public static void Main(string[] args)
    {
        FileInfo[] files = 
            new DirectoryInfo(@"D:\downloads\_Installs").GetFiles();
        ArrayList exefiles = new ArrayList();

        foreach (FileInfo f in files)
        {
            if (f.Extension == ".exe") // or whatever matching you want to do.
            {
                exefiles.Add(f);
            }
        }

        foreach (FileInfo f in exefiles)
        {
            Console.WriteLine(f.FullName);
        }
        Console.ReadKey();
    }
}

VS2008

public class GetFiles
{
    public static void Main(string[] args)
    {
        FileInfo[] files = 
            new DirectoryInfo(@"D:\downloads\_Installs").GetFiles();

        var exefiles = from FileInfo f in files 
                       where f.Extension == ".exe" 
                       select f;

        foreach (FileInfo f in exefiles)
        {
            Console.WriteLine(f.FullName);
        }

        Console.ReadKey();
    }
}

Easiest way to is load them all into a List<> and then use Linq to filter out the ones you want.

VS2005

public class GetFiles
{

    public static void Main(string[] args)
    {
        FileInfo[] files = 
            new DirectoryInfo(@"D:\downloads\_Installs").GetFiles();
        ArrayList exefiles = new ArrayList();

        foreach (FileInfo f in files)
        {
            if (f.Extension == ".exe") // or whatever matching you want to do.
            {
                exefiles.Add(f);
            }
        }

        foreach (FileInfo f in exefiles)
        {
            Console.WriteLine(f.FullName);
        }
        Console.ReadKey();
    }
}

VS2008

public class GetFiles
{
    public static void Main(string[] args)
    {
        FileInfo[] files = 
            new DirectoryInfo(@"D:\downloads\_Installs").GetFiles();

        var exefiles = from FileInfo f in files 
                       where f.Extension == ".exe" 
                       select f;

        foreach (FileInfo f in exefiles)
        {
            Console.WriteLine(f.FullName);
        }

        Console.ReadKey();
    }
}
听你说爱我 2024-07-23 02:07:30

您需要做的是在将列表绑定到中继器控件之前过滤掉您不希望显示的所有图像。

What you will need to do is filter out all the images you do not wish to display from your list before you bind it to your repeater control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文