Sorting is a neat feature that can be done to lists for easier accessibility. Sorting lists in alphabetical order has kind of become a norm in our society, people are used to it, so naturally as programmers we would like to display our data in alphabetical order when possible. This tutorial will show you how to easily sort a list box in alphabetical order making it easier for users to view the data.

Setting Up

If you have not done so already, we need to set up a web site and web form to work on. Click on File > New > Web Site. Using ‘Visual Basic’ as the template, select ASP.NET Empty Web Site, name the web site anything you’d like and click OK. Now look in the solution explorer window and you will see the web site we just created, right click it and select Add New Item. Once again, use ‘Visual Basic’ as the template and select Web Form. Leave the file name as Default.aspx and click add. Now that we have a web site and web form to work on, we can begin coding.

Step One

Open Default.apsx in source view so we can see the html side of things of our web form. Expand the ‘Toolbox’ panel and place a ListBox item inside the ‘div’ tags. Now place a Button item after the closing list box tag and change it’s text to Sort.

Now lets add items to the listbox, we will use classic video games as our topic of choice for the list. To do this we must use the ListItem template inside the listbox tags with the name of an item inside of the list item templates. Look in the code snippet below for a better understanding and apply the same items in your list box.

Step Two

If you are not already on Default.aspx, do so and switch to design view to create a event handler in Default.aspx.vb. You will see a private sub called Button1_Click and inside of this sub is where we will insert code that will sort the list box. For this specific example we will create a private sub and then call it inside the ‘Button1_Click’ sub. So whenever we want to sort a list we can just call the sub rather than writing tedious code. Apply the data in the code snippet below to the Default.aspx.vb page:

In the sortListBoxItems private sub, we declare a sorted list and add the contents in the list to ti. We then declare list.DataSource to equal the newly created sorted list. Now we can sort the list box by defining a ‘DataValueField’, ‘DataTextField’, and data binding. Data binding pretty much secures controls to data from databases and is commonly used when working with databases.


Run the page by clicking on the green arrow near the top or pressing F5. If it asks you to ‘Modify the Web.config file to enable debugging’ click OK. You will see the list box and the sort button. Notice how the list box is not sorted in alphabetical order, so click on the Sort button and like magic, the list box is now sorted alphabetically.

not sorted

So now you know how to sort a list box. Organizing data by certain attributes is very common, whether it be alphabetical, numerical, or maybe by how many items are contained within it. No matter what, good organization of data is key in any application or web site so utilize it whenever possible.

Download Source Files