asp.net listbox sort alphabetically
this tutorial help you to create a sorted listbox in asp.net. you can make a sorted
listbox manually by placing list items alphabetically. in asp.net listbox there are no built in
feature to sort listbox list items. so we need to apply some techniques to populate a sorted listbox.
We also need to ensure that the sorting will generate only one time when the page load first time. here we create a generic list with listbox list items. then sort it's items using linq technology. then clear the listbox. at last we populate the listbox by sorted generic list.
We also need to ensure that the sorting will generate only one time when the page load first time. here we create a generic list with listbox list items. then sort it's items using linq technology. then clear the listbox. at last we populate the listbox by sorted generic list.
listbox-sort-alphabetically.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
List<ListItem> list = new List<ListItem>(ListBox1.Items.Cast<ListItem>());
//sort list item alphabetixcally
list = list.OrderBy(x=> x.Text).ToList<ListItem>();
ListBox1.Items.Clear();
ListBox1.Items.AddRange(list.ToArray<ListItem>());
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = "you selected....<br />";
Label1.Text += "item: " + ListBox1.SelectedItem.Text;
Label1.Text += "<br />value: " + ListBox1.SelectedItem.Value;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net listbox sort alphabetically</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - listbox sort alphabetically
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Text="select an item from ListBox."
Font-Size="X-Large"
Width="350"
>
</asp:Label>
<br /><br />
<asp:ListBox
ID="ListBox1"
runat="server"
AutoPostBack="true"
Width="350"
Font-Size="X-Large"
SelectionMode="Single"
Height="200"
OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
>
<asp:ListItem Text="Rufous-tailed Plantcutter" Value="1"></asp:ListItem>
<asp:ListItem Text="Andean Cock-of-the-rock" Value="2"></asp:ListItem>
<asp:ListItem Text="Yellow-bellied Elaenia" Value="3"></asp:ListItem>
<asp:ListItem Text="Grey-hooded Flycatcher" Value="4"></asp:ListItem>
<asp:ListItem Text="Cliff Flycatcher" Value="5"></asp:ListItem>
</asp:ListBox>
</div>
</form>
</body>
</html>


- How to sort ListBox items
- How to sort ListBox items by value
- How to display different tooltip on ListBox items
- How to show horizontal ScrollBar in a ListBox
- How to check whether an item exists in a ListBox
- How to add attributes to a ListBox items
- How to select all items in a CheckBoxList
- How to retrieve multiple selected values from CheckBoxList
- How to create a horizontal CheckBoxList
- How to show ScrollBar in a CheckBoxList