asp.net sort dropdownlist by value
The following asp.net c# example code demonstrate us how can we sort DropDownList items
by its item value. In this tutorial we will sort the DropDownList items programmatically at run time using c# code.
By default, DropDownList Class have no built in property or method to sort its items. To sort DropDownList items, first we need to initialize a Generic List which data type is ListItem. Then we populate Generic List by items from DropDownList. Next, we call Generic List OrderBy() method to sort its items. We sort the Generic List items based on ListItem object's 'Value' property value. In this tutorial we sorted DropDownList items by its item value ascending order.
Finally, we remove all items from DropDownList server control and we repopulate it with Generic List items. Now the browser render DropDownList items sorted on ascending order based on its items value.
By default, DropDownList Class have no built in property or method to sort its items. To sort DropDownList items, first we need to initialize a Generic List which data type is ListItem. Then we populate Generic List by items from DropDownList. Next, we call Generic List OrderBy() method to sort its items. We sort the Generic List items based on ListItem object's 'Value' property value. In this tutorial we sorted DropDownList items by its item value ascending order.
Finally, we remove all items from DropDownList server control and we repopulate it with Generic List items. Now the browser render DropDownList items sorted on ascending order based on its items value.
sort-dropdownlist-by-value.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>();
Label1.Text = "";
foreach (ListItem li in DropDownList1.Items)
{
//add ListItem to generic list
list.Add(li);
Label1.Text += li.Text + " = " + li.Value + "<br />";
}
//sort list items by item value alphabetically/ascending
List<ListItem> sorted = list.OrderBy(b => b.Value).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 sort dropdownlist by value</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - sort dropdownlist by value
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Text="select an item from dropdownlist."
Font-Size="X-Large"
Width="350"
>
</asp:Label>
<br /><br />
<asp:DropDownList
ID="DropDownList1"
runat="server"
AutoPostBack="true"
Width="350"
Font-Size="X-Large"
Font-Names="Comic Sans MS"
>
<asp:ListItem Text="Secretary Bird" Value="4"></asp:ListItem>
<asp:ListItem Text="Osprey" Value="3"></asp:ListItem>
<asp:ListItem Text="White-tailed Eagle" Value="5"></asp:ListItem>
<asp:ListItem Text="Egyptian Vulture" Value="2"></asp:ListItem>
<asp:ListItem Text="Black Kite" Value="1"></asp:ListItem>
<asp:ListItem Text="Pied Harrier" Value="1"></asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>

- How to add attributes to DropDownList items
- How to make DropDownList first item not selectable
- How to create a rounded corners DropDownList
- How to sort DropDownList items
- How to sort DropDownList items alphabetically
- How to sort DropDownList items by text
- How to sort DropDownList items in descending order
- How to select DropDownList item by value
- How to select DropDownList item by item text
- How to create a DropDownList displaying years