【问题标题】:Insert whitespace between characters in listbox在列表框中的字符之间插入空格
【发布时间】:2016-06-23 22:32:08
【问题描述】:

我正在尝试将项目插入到我的 asp.net C# 应用程序的列表框中

我连接了一些值并在它们之间放置了空格,但它没有显示在列表框中。

        ListItem lt = new ListItem();
        lt.Text = ItemName + "    " + barcode + "    " + price; // problem
        lt.Value = barcode;
        lstMailItems.Items.Add(lt);

我什至尝试过

lt.Text = ItemName + "\t\t" + barcode + "\t\t" + price; // problem
lt.Text = ItemName + "& nbsp;" + barcode + "& nbsp;" + price; // &nbsp shows up as text

但这似乎不起作用。如何在这些字符串之间放置空格,以便它也显示在列表框中

【问题讨论】:

  • 我检查了浏览器渲染的 HTML,生成了空格,我认为它是由浏览器修剪的。

标签: c# asp.net listbox


【解决方案1】:
string spaces = Server.HtmlDecode("    "); 

lt.Text = ItemName + spaces + barcode + spaces + price; // works

【讨论】:

  • 太好了,我一直在谷歌上搜索这个!
【解决方案2】:

我遇到了同样的问题,上面的答案让我找到了这个对我有用的问题。

string space = " ";
                space = Server.HtmlDecode(space);
                line = line.Replace(" ", space);
                ClassCodeListBox.Items.Add(line);

【讨论】:

    【解决方案3】:

    试试

    lt.Text = string.Format("{0}\&nbsp\;{1}\&nbsp\;{2}",ItemName,barcode,price);

    如果看不到,请将 \ 替换为 &nbsp

    或者

    lt.Text = string.Format("{0} {1} {2}",ItemName,barcode,price);

    【讨论】:

    • @Myra:他需要列表项中的空格
    • 您是否在页面中使用 css 的 white-spacing 属性?
    【解决方案4】:

    这里有两个运行良好的示例,以及如何获取当前格式:

      var SaleItem = new
        {
            name = "Super Cereal",
            barcode = "0000222345",
            price = 2.55m
        };
    
        ListItem lt = new ListItem();
        string space = " ";
        lt.Text = String.Concat(SaleItem.name, 
            space, SaleItem.barcode, space, SaleItem.price);
        lt.Value = SaleItem.barcode;
    
        ListItem lt2 = new ListItem();
        lt2.Text = string.Copy(String.Format("{0}: {1} {2}", 
                   SaleItem.name, SaleItem.barcode, SaleItem.price.ToString("C")));
        lt2.Value = SaleItem.barcode;
    
        lstMailItems.Items.Add(lt);
        lstMailItems.Items.Add(lt2);
    

    【讨论】:

      猜你喜欢
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多