【问题标题】:How to remove undefined array in asp.net?如何删除 asp.net 中未定义的数组?
【发布时间】:2014-04-21 12:49:44
【问题描述】:
  • 我正在尝试选择文件夹内容
  • 在该文件夹中包含图像,当我选择该文件夹时,它将选择所有内容。
  • 另外它正在使用 img src undefind
  • 所以我需要删除,如果使用 asp.net c# 未定义 src,我该如何删除

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

文件隐藏代码:

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

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    假设您有一个 C# 中的图像 src 值列表,您可以使用 LINQ 过滤掉其中任何未定义的值:

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

    lambda value =&gt; !string.IsNullOrWhitespace(value) 过滤 list 使得每个元素都不是 null 或空白(我假设您的意思是未定义)。

    这有帮助吗?如果您可以分享一些您现在拥有的示例代码,我们可以提供更多帮助。

    【讨论】:

  • 您能否分享生成该代码的 ASP.NET 代码?
  • 你能在后面调试代码并发布subdirectoryEntriesssflen 是什么吗?另外,为什么在 for 循环中除以 3?
  • 我的电脑中有 img 文件夹,它从该文件夹中获取 img 并在循环中除以 3 意味着在每行中创建 3 个 imgs k。当我在该文件夹中选择包含 5 个图像的文件夹时,第一行填充机智 3 个 img,第二行仅填充 2 个 img 一个 img 框是空的,所以我需要删除它
  • 猜你喜欢
    • 2015-04-20
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    相关资源
    最近更新 更多