<%@ 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>());
listlist = 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 C**k-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>
this example may help you