Auto Generate Row Number in ASP.NET ListView

One of my blog readers Robert asked me a question. Is it possible to create a counter field or an auto-generated Row Number in an ASP.NET ListView.

The solution is to add <%# Container.DataItemIndex + 1 %> to the ItemTemplate of the ListView as shown below:

ListView Row Number

The output with the auto generated serial number/row number is as shown below:

ListView Row Number

If you are working with the ASP.NET GridView, check this post of mine ASP.NET GridView Tips and Tricks series



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

4 comments:

Edward said...

Thanks, this is just what I was looking for!

nhp91 said...

Thank you, i'm looking for this

Anonymous said...

OMG, i was two days looking for a solution for my problem (using radiobutton to select a row in a listview and get the row index clicking in a button) and this is just what I was looking for.

ஐயப்பா கம்ப்யுட்டா்ஸ் said...

Thank You Sir!