简体   繁体   English

在ASP.NET C#中显示项目列表

[英]Displaying List of items in asp.net C#

Greetings, I ran into an issue on how to display items from my list. 问候,我遇到了一个关于如何显示列表中项目的问题。 I have looked online but the only code was in c# with console.writeline command. 我在网上看过,但唯一的代码是用console.writeline命令在C#中进行的。 I also tried this: 我也试过这个:

Response.Write(Convert.ToString(orderList[i]) + "<br \\");

and

lblOrder.Text = Convert.ToString(orderList[i]);

however it doesn't work. 但是它不起作用。

here is my code: 这是我的代码:

    if(getOrangeTotal != 0) {
        orderList.Add(getOrangeTotal);
    }
    if (getBreadTotal != 0) { 

        orderList.Add(getBreadTotal);
    }

    sizeOfList = orderList.Count;

    for (int i = 0; i < sizeOfList; i++)
    {
        //lblOrder.Text = Convert.ToString(orderList[i]);
        //Response.Write(Convert.ToString(orderList[i]) + "<br \\");
    }

Explanation: The code gets values from Session Variables from previous page and I want to display values that don't have value of 0 in them. 说明:代码从上一页的会话变量中获取值,我想显示其中不包含0的值。

From your example, you appear to be displaying (or attempting to display) an entire list within a single control (a label) by delimiting the items with <BR> tags. 在您的示例中,您似乎通过使用<BR>标记定界了单个控件(标签)中的整个列表(或试图显示)。 That is a pretty bizarre way to go about it. 那是一个很奇怪的方法。 It is no wonder you are fighting the technology. 难怪您正在与这项技术作斗争。

Generally speaking, if you have a list of items to display, you would use something like a Table or Repeater control (on web forms) or a simple foreach (in MVC). 一般来说,如果您要显示的项目列表,则可以使用TableRepeater控件(在Web窗体上)或简单的foreach (在MVC中)之类的东西。 Or you could bind the items to a DropDownList if you want the user to be able to choose one. 或者,如果希望用户能够选择一项,则可以将项目绑定到DropDownList

If you truly know what you are doing and want to put everything into one controls's Text property, you could use a little LINQ: 如果您真的知道自己在做什么,并且想要将所有内容放入一个控件的Text属性中,则可以使用一些LINQ:

litFoo.Text = string.Join
              (
                  "<br>", 
                  list.Select( a => a.ToString() )
              );

Note that litFoo in this example is a Literal which won't have the same sort of escaping issues as a Label control. 请注意,本示例中的litFoo是Literal,不会像Label控件一样具有转义问题。

BTW <BR> is a self closing entity so you don't need to worry about any slashes . BTW <BR>是一个自闭实体,因此您不必担心任何斜线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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