English 中文(简体)
如何在ASP.NET Repeater中仅显示文件夹中的某些图像。
原标题:
  • 时间:2009-04-07 10:10:25
  •  标签:

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.

我的中继器 (wǒ de zhōng jì qì)

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

    }

我代码后面 - 重复项数据绑定事件代码

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();
        }
最佳回答

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

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

EDIT:

不是拥有:

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 ));
}
问题回答

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

VS2005: Visual Studio 2005

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是Visual Studio 2008的缩写。

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

您需要做的是在将列表绑定到重复控件之前从中筛选出所有不想显示的图像。





相关问题
热门标签