views:

2017

answers:

3

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


        }

    }

Thanks in advanced Etienne

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

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.

Andy Rose
How would i do this in code?
Etienne
+3  A: 

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 ));
}
ChrisF
They can be in the format of .png or .jpg
Etienne
Where must i place that code. Or what must i replace? Sorry i am not that good with C#.
Etienne
@Etienne - I'm not too hot on Regex, but there'll be a regular expression you can use to for that - I'll do a search and see if I can find it
ChrisF
Thanks for this ChrisF , but for some reason my Repeater is still returning all my Images in my Folder. Must i not change anything inside my ItemDataBound event?
Etienne
@Etienne - Can you post your updated Page_Load code?
ChrisF
Done, had to restart my PC, something was not working properly. Now its not returning any images with your Edit 2 code.
Etienne
Thanks! i got it, i had to add this oList.Add(System.IO.Path.GetFileName(filter)); in my loop
Etienne
Only just got back from lunch so didn't see your comment. Glad it's working now. It'd be great if you accepted the answer.
ChrisF
Only thing i have noticed is. When i use this.......string[] extensions = { "*.jpg" };...........it does not return any images at all. Any idea?
Etienne
Does my latest revision make more sense? I can't see why only having one element in the array doesn't work. Have you tried stepping through on a debugger?
ChrisF
Got it thanks - tell me.....What code changes must i make to let my code look at a hyperlink for the image and not the "pics" folder? Any Idea? Example: http://www.erate.co.za/imgGrab.aspx?Id=99 ---- thats where the image is located and not in my "pics" folder.
Etienne
This is where i posted the question http://stackoverflow.com/questions/728862/getting-image-from-hyperlink-and-not-from-folder-to-populate-my-repeater-control
Etienne
+2  A: 

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();
    }
}
peiklk
Cant use Linq because i am using VS 2005
Etienne
Added VS2005 version...
peiklk