Display list of results with numbers in ASP.NET

I have a common problem in an ASP.NET web form. I am simply surprised that I did not find a general solution :)

I have a list of results returned from a SQL Server database. I want to show these results on my web page with the number to their left. For example, my result set might look like this:

New York | NY
Chicago | IL
Los Angeles | CA

On my webpage, I would like to show them as:

1. New York, NY
2. Chicago, IL
3. Los Angeles, CA

I was surprised that I could not find a way to automatically display numbers. Am I doing something wrong? If not, what is the recommended approach?

thank

+3
source share
6 answers

WebForms, BulletedList. BulletedList (, SqlDataSource LinqDataSource) DataSource.

BulletedList , BulletStyle Numbers.

BulletedList SqlDataSource: http://aspalliance.com/247_ASPNET_v20_Introducing_BulletedList_Control.4

+1

HTML , , - ASP.NET. HTML- <ol> <li> .

ASP.NET , Repeater: http://msdn.microsoft.com/en-us/magazine/cc163780.aspx. : http://www.jigar.net/howdoi/viewhtmlcontent194.aspx

http://www.htmlcodetutorial.com/_OL.html:

<OL>
<LI>Take 495 north
<LI>Cross the 14th Street Bridge
<LI>Take the Maine Avenue exit
<LI>Turn left at the first light
</OL>
+2

(<ol>) HTML:

<ol>
<% foreach(var item in theList) { %>
    <li><%= item %></li>
<% } %>
</ol>
+1

. . , , , -.

, , , . LINQ. , . .

- , :

        List<string> myData = new List<string>()
        {
            "New York | NY",
            "Chicago | IL",
            "Los Angeles | CA"
        };

... , .

myData = myData.Select((s, i) => i.ToString() + ". " + s).ToList();

, , , , "1." "(1)" "|" ",".

, , City, CityName CityAbbreviation...

    class City
    {
        public string CityName { get; set; }
        public string CityAbbreviation { get; set; }
    }

... . , . , .

            // Create fake data.
            List<City> myData = new List<City>()
            {
                new City(){ CityName = "New York", CityAbbreviation="NY" },
                new City(){ CityName = "Chicago", CityAbbreviation="IL" },
                new City(){ CityName = "Los Angeles", CityAbbreviation="CA" }
            };

            // Project data...  We create a new container object
            // to hold each city in one property
            // and a NUMBER in a second property.
            var anonList = myData.Select((x, i) => new 
            {
                Number = (i + 1).ToString(),
                City = x
            }).ToList();

            // Databind (Winforms example)
            listBox1.Items.Clear();
            anonList.ForEach(
                c => 
                listBox1.Items.Add(
                    c.Number + ". " + c.City.CityName + " , " + c.City.CityAbbreviation
                )
            );

, , , (City, CityName). , City, .

            // Create fake data.
            List<string> myStringData = new List<string>()
            {
                "New York | NY",
                "Chicago | IL",
                "Los Angeles | CA"
            };

            // Convert strings to City objects            
            List<City> myData = myStringData.Select(
                s => {
                    List<string> parts = s.Split('|').Select(s2 => s2.Trim()).ToList();
                    return new City()
                        { CityName = parts[0], CityAbbreviation = parts[1] };
                }).ToList();
+1
source

Use ol, li:

<ol>
    <li>New York, NY</li>
    <li>Chicago, IL</li>
    <li>Los Angeles, CA</li>
</ol>
0
source

How do you iterate over a result set?

I could be wrong, but I would like to do this with a for loop.

First declare a list on the front side

<ol runat="server" id="orderedList"></ol>

for(int i = 0; i < results.Length; i++)
{
    orderedList.InnerHtml += "<li>" + results[i].ToString() + "</li>";
}
0
source

Source: https://habr.com/ru/post/1764765/


All Articles