0

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.

MrDarkness96
  • 57
  • 2
  • 13
  • what is this `Response.Write(Convert.ToString(orderList[i]) + "
    – MethodMan Dec 09 '17 at 23:17
  • @MethodMan this is a web-based application, i want to display all of the content from the list in a list going down. – MrDarkness96 Dec 09 '17 at 23:20
  • you mean a `dropdownlist`? – MethodMan Dec 10 '17 at 00:13
  • 1
    I would highly suggest that you do a google search on the following `C# stackoverflow populating a dropdownlist from database` or do the same search on how to `Bind List to a dropdownlist` also read up on `PostBacks` and understand the life cycle of an `ASP.NET WebPage. – MethodMan Dec 10 '17 at 00:15

1 Answers1

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

John Wu
  • 50,556
  • 8
  • 44
  • 80