asp.net dropdownlist sort by text
The following asp.net c# example code demonstrate us how can we sort a DropDownList items by its item's text.
Each ListItem object have a Text property and a Value property. We can sort DropDownList items by based on both
'Text' property value or 'Value' property value.
DropDownList class does not support direct sorting of its items. To sort a DropDownList we need to write few lines of code. First we initialize a Generic List with data type ListItem. Then we populate the Generic List from DropDownList items. After populating the Generic List with items, we can sort those items by calling OrderBy() method. We sort the Generic List items based on ListItem object's Text property value. In this example code we sorted DropDownList items on ascending order.
Finally we need to remove all items from DropDownList and populate it again with Generic List sorted items. This is the process we applied to sort DropDownList items based on item's text.
DropDownList class does not support direct sorting of its items. To sort a DropDownList we need to write few lines of code. First we initialize a Generic List with data type ListItem. Then we populate the Generic List from DropDownList items. After populating the Generic List with items, we can sort those items by calling OrderBy() method. We sort the Generic List items based on ListItem object's Text property value. In this example code we sorted DropDownList items on ascending order.
Finally we need to remove all items from DropDownList and populate it again with Generic List sorted items. This is the process we applied to sort DropDownList items based on item's text.
dropdownlist-sort-by-text.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//this line create a generic list data type ListItem
List<ListItem> list = new List<ListItem>();
foreach (ListItem li in DropDownList1.Items)
{
//add ListItem to generic list
list.Add(li);
}
//sort list items by item text alphabetically/ascending
List<ListItem> sorted = list.OrderBy(b => b.Text).ToList();
//empty dropdownlist
DropDownList1.Items.Clear();
//repopulate dropdownlist with sorted items.
foreach (ListItem li in sorted)
{
DropDownList1.Items.Add(li);
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net dropdownlist sort by text</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - dropdownlist sort by text
</h2>
<hr width="550" align="left" color="Gainsboro" />
<br /><br />
<asp:DropDownList
ID="DropDownList1"
runat="server"
AutoPostBack="true"
Width="350"
Font-Size="X-Large"
Font-Names="Comic Sans MS"
>
<asp:ListItem Text="Turkey Vulture" Value="1"></asp:ListItem>
<asp:ListItem Text="Eurasian Hobby" Value="2"></asp:ListItem>
<asp:ListItem Text="Northern Crested Caracara" Value="3"></asp:ListItem>
<asp:ListItem Text="American Kestrel" Value="4"></asp:ListItem>
<asp:ListItem Text="Peregrine Falcon" Value="5"></asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>

- How to get DropDownList selected value by selectedindexchanged event
- How to add attributes to DropDownList items
- How to add CSS class to DropDownList each items
- How to make DropDownList first item not selectable
- How to display tooltip for DropDownList each items
- How to sort DropDownList items
- How to sort DropDownList items alphabetically
- How to sort DropDownList items by value
- How to select DropDownList item by item text
- How to create a DropDownList displaying years