遍历SortedList集合元素——使其顺序按字母排序(汉字则按拼音首字母排序)
郁闷,昨天发了居然被我误删了,今天重新再发。
使用SortedList集合对象,方便的对DropdownList控件的下来列表元素进行按字母排序,汉字则按拼音首字母排序,这个功能还不错。
SortedList.aspx
<%@Page Language="c#" debug="true" %>
<script runat="server" Language="c#">
void Page_Load(object source, EventArgs e)
{
SortedList mySortedList = new SortedList();
mySortedList["张三"]="我是张三";
mySortedList["李四"]="我是李四";
mySortedList["王五"]="我是王五";
mySortedList["赵六"]="我是赵六";
if (!(Page.IsPostBack))
{
foreach (DictionaryEntry Item in mySortedList)
{
ListItem newListItem = new ListItem();
newListItem.Text = Item.Key.ToString();
newListItem.Value = Item.Value.ToString();
myDropDownList.Items.Add(newListItem);
}
}
}
void Click(object source, EventArgs e)
{
myLabel.Text = myDropDownList.SelectedItem.Value;
}
</script>
<html>
<form runat="server">
Pick a word from the list:
<asp:dropdownlist id="myDropDownList" runat="server" />
<asp:button id="myButton" runat="server" text="OK" Onclick="Click" />
<br /><br />
<b>Definition: </b>
<asp:Label id="myLabel" runat="server" text="" />
</form>
</html>
<script runat="server" Language="c#">
void Page_Load(object source, EventArgs e)
{
SortedList mySortedList = new SortedList();
mySortedList["张三"]="我是张三";
mySortedList["李四"]="我是李四";
mySortedList["王五"]="我是王五";
mySortedList["赵六"]="我是赵六";
if (!(Page.IsPostBack))
{
foreach (DictionaryEntry Item in mySortedList)
{
ListItem newListItem = new ListItem();
newListItem.Text = Item.Key.ToString();
newListItem.Value = Item.Value.ToString();
myDropDownList.Items.Add(newListItem);
}
}
}
void Click(object source, EventArgs e)
{
myLabel.Text = myDropDownList.SelectedItem.Value;
}
</script>
<html>
<form runat="server">
Pick a word from the list:
<asp:dropdownlist id="myDropDownList" runat="server" />
<asp:button id="myButton" runat="server" text="OK" Onclick="Click" />
<br /><br />
<b>Definition: </b>
<asp:Label id="myLabel" runat="server" text="" />
</form>
</html>