简体   繁体   中英

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

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. 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). Or you could bind the items to a DropDownList if you want the user to be able to choose one.

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:

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.

BTW <BR> is a self closing entity so you don't need to worry about any slashes .

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