简体   繁体   中英

How to remove undefined array in asp.net?

  • I am trying to select folder content
  • In that folder contain images when I am select that folder it will select all content.
  • plus it is taking img src undefind
  • so I need to remove that how can I remove if src is undefined using asp.net c#

     <div id="cell" class="box2"> <a href="undefined"> <img width="260px" height="135px" src="undefined" alt="" style="box-shadow: 1px 2px 2px #BDBDBD; border: 1px solid #D1D1D1;"> </img> </a> </div> 

Code behind file:

protected void chbindustry_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (result == false)
            {

                string[] subdirectoryEntries = Directory.GetDirectories(Server.MapPath("BusinessCards"));
                string f;
                string[] ss;
                string side = chklist.SelectedValue;// RadioButtonList1.SelectedValue;
                foreach (ListItem li in chbindustry.Items)
                {
                    if (li.Selected)
                    {

                        ss = li.Text.Split('(');

                        f = Server.MapPath("BusinessCards").ToString() + "\\" + ss[0];
                        int c = f.Count();
                        DirectoryInfo d = new DirectoryInfo(f);
                        int len = d.GetFiles().Length;
                        for (int i = 1; i <= d.GetFiles().Length / 3; i++)
                        {
                            Page.ClientScript.RegisterArrayDeclaration("ImgPaths", "'" + "BusinessCards/" + f.Remove(0, f.LastIndexOf('\\') + 1) + "/" + i + ".jpg'");
                            Page.ClientScript.RegisterArrayDeclaration("refs", "'" + "DesignBCs.aspx?img=BusinessCards/" + f.Remove(0, f.LastIndexOf('\\') + 1) + "/" + i + "&Side=" + side + "'");
                        }

                    }
                }
            }
            result = true;
        }

Assuming you have a list of the image src values in C#, you could use LINQ to filter out any of them that are undefined:

var list = some code to get a list of img srcs from your folder;
list = list.Where(value => !string.IsNullOrWhitespace(value)).ToList();

The lambda value => !string.IsNullOrWhitespace(value) filters list such that each element is not null or whitespace (what I'm assuming you mean by undefined).

Does this help? If you can share some sample code of what you have now we can help more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM