locked
With Directory.GetFiles, how multiple add the path and how to show hyperlink inside the Container.DataItem ? RRS feed

  • Question

  • User-1551112854 posted

    With Directory.GetFiles, how multiple add the path and how to show hyperlink inside the Container.DataItem ? Thats Code do not work and the responding message "System.Web.HttpException: 'DataBinding: 'System.String', image
    does not contain a property named.'"

    /* aspx.cs */

    string[] files = Directory.GetFiles(Server.MapPath("~/path1"));
    string[] files2 = Directory.GetFiles(Server.MapPath("~/path2"));
    List<String> image = new List<string>(files.Count());
    string[] imagelength = { ".jpg", ".jpeg" };
    List<String> image2 = new List<string>(files2.Count());
    string[] imagelength2 = { ".jpg", ".jpeg" };
    foreach (string file in files)
    {
    for (int i = 0; i < imagelength.Length; i++)

    {
    string[] bit;
    bit = file.Split('.');
    if (imagelength[i] == "." + bit[1])
    image.Add(String.Format("~/path1/{0}", System.IO.Path.GetFileName(file)));
    }
    }
    foreach (string file in files2)
    {
    for (int i = 0; i < imagelength2.Length; i++)

    {
    string[] bit;
    bit = file.Split('.');
    if (imagelength2[i] == "." + bit[1])
    image2.Add(String.Format("~/path2/{0}", System.IO.Path.GetFileName(file)));
    }
    }

    itemgalery.DataSource = image;
    itemgalery.DataBind();
    itemgalery.DataSource = image2;
    itemgalery.DataBind();

    /* aspx */

    <asp:DataList ID="itemgalery" runat="server" RepeatColumns="3" HorizontalAlign="Left" BorderStyle="None" BorderWidth="0" ItemStyle-BorderStyle="None" ItemStyle-BorderColor="Transparent" ItemStyle-BorderWidth="0">
    <ItemTemplate>

    <asp:HyperLink ID="HyperLink2" runat="server"
    ImageUrl='<%# DataBinder.Eval(Container.DataItem, "image")%>'
    NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "image2") %>'
    onclick="return gp.expand(this)" BorderStyle="None"></asp:HyperLink>

    </ItemTemplate>
    </asp:DataList>

    Friday, June 28, 2019 8:57 AM

All replies

  • User-943250815 posted

    You created a List of String, so the is no property "image" or "image2".
    In HyperLink control just use Container.DataItem

    <asp:HyperLink ID="HyperLink2" runat="server"
    ImageUrl='<%# Container.DataItem%>'
    NavigateUrl='<%# Container.DataItem %>'
    onclick="return gp.expand(this)" BorderStyle="None"></asp:HyperLink>

    Suggest you take a look in DirectoryInfo.GetFiles and FileInfo

    Friday, June 28, 2019 12:17 PM
  • User665608656 posted

    Hi Goktug Pala,

    As suggested by jzero, this is a good choice.

    But I found that your ImageUrl and NavigateUrl attributes refer to different image values, and you bind datalist twice in the code behind.

    I want to confirm that: do you want to show the image in image1, click on the image, and then expand the link image in image2?

    If so, I suggest you create a class in the code behind, put image1 and image2 together in a class, and get the corresponding value in the foreground through:

    <asp:HyperLink ID="HyperLink2" runat="server"
                            ImageUrl='<%#Eval("Image1") %>'
                            NavigateUrl='<%#Eval("Image2") %>'
                            onclick="return gp.expand(this)" BorderStyle="None"></asp:HyperLink>

    code behind:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    string[] files = Directory.GetFiles(Server.MapPath("~/path1"));
                    string[] files2 = Directory.GetFiles(Server.MapPath("~/path2"));
                    List<String> image = new List<string>(files.Count());
    string[] imagelength = { ".jpg", ".jpeg" };
    List<String> image2 = new List<string>(files2.Count());
    string[] imagelength2 = { ".jpg", ".jpeg" }; foreach (string file in files) { for (int i = 0; i < imagelength.Length; i++) { string[] bit; bit = file.Split('.'); if (imagelength[i] == "." + bit[1]) image.Add(String.Format("~/path1/{0}", System.IO.Path.GetFileName(file))); } } foreach (string file in files2) { for (int i = 0; i < imagelength2.Length; i++) { string[] bit; bit = file.Split('.'); if (imagelength2[i] == "." + bit[1]) image2.Add(String.Format("~/path2/{0}", System.IO.Path.GetFileName(file))); } } List<Images> imagelists = new List<Images>(); for (int i = 0; i < image.Count; i++) { Images ll = new Images(); ll.Image1 = image[i].ToString(); ll.Image2 = image2[i].ToString(); imagelists.Add(ll); } itemgalery.DataSource = imagelists; itemgalery.DataBind(); } } public class Images { public string Image1 { get; set; } public string Image2 { get; set; } }

    If not,you can follow jzero's advice.

    Here is the result of my work demo:

    Best Regards,

    YongQing.

    Monday, July 1, 2019 3:08 AM
  • User-1551112854 posted

    thank you very much dear my friend.. It's work like flying birds :)

    Monday, July 1, 2019 6:25 AM
  • User-1551112854 posted

    Hi Yongqing Yu,

    Today I publish my site and copy the server root.. And script not work on server side. I'm so worry about that.. and I return to my old work 

    Tuesday, July 16, 2019 5:29 PM
  • User665608656 posted

    Hi Goktug,

    According to your description, I can not clearly understand the issue you are facing.

    Can't you find the picture path? If so,have you published the pictures together to the server?

    The following are my folders before and after publication. The Image1 and Image2 folder are the folders where I need to show the pictures.

    If not ,I hope you can describe the issue in detail. I suggest you can use screenshots to show your issue.

    Best Regards,

    YongQing.

    Wednesday, July 17, 2019 2:36 AM