ASP.Net/Collections/SortedList
Версия от 15:30, 26 мая 2010; (обсуждение)
Sorted List Demo (C#)
<%@ Page Language="C#" %>
<script runat="server">
void Page_Load()
{
SortedList stlShippers = new SortedList();
stlShippers["cp"]="CP";
stlShippers["fe"]="FE";
stlShippers["us"]="US";
lblOut.Text = "Your selection = " + stlShippers[txtCodeIn.Text];
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
Please enter a shipper code ("cp" "fe" "us"):
<asp:TextBox runat="server" ID="txtCodeIn"></asp:TextBox><br>
<asp:Button runat="server" Text="Button"></asp:Button><br>
<asp:Label runat="server" ID = "lblOut">Label</asp:Label>
</form>
</body>
</html>
SortedList Demo (VB.net)
<%@ Page Language="VB" %>
<script runat="server">
Dim ColorList(6) as String
Dim FontList as new ArrayList()
Dim QuoteList as new Sortedlist()
Sub Page_Load
ColorList(0) = "Red"
ColorList(1) = "Orange"
ColorList(2) = "Yellow"
ColorList(3) = "Green"
ColorList(4) = "Blue"
ColorList(5) = "Indigo"
ColorList(6) = "Violet"
FontList.Add("Times New Roman")
FontList.Add("Arial")
FontList.Add("Verdana")
FontList.Add("Comic Sans MS")
QuoteList.Add ("Quotation", "Quotation. ")
QuoteList.Add ("Joke", "Joke")
QuoteList.Add ("Wisdom", "Wisdom")
QuoteList.Add ("Saying", "Saying")
If Not Page.IsPostback
Dim ColorName as String
For Each ColorName in ColorList
ddlColorList.Items.Add(ColorName)
Next
ddlFontList.DataSource = FontList
ddlFontList.DataBind()
ddlQuoteList.DataSource = QuoteList.Keys
ddlQuoteList.DataBind()
End If
End Sub
Sub btnSelectColor_Click(sender As Object, e As EventArgs)
lblOutputMessage.Text = QuoteList(ddlQuoteList.SelectedItem.Text)
lblOutputMessage.ForeColor = _
System.Drawing.Color.FromName(ddlColorList.SelectedItem.Text)
lblOutputMessage.Font.Name = _
ddlFontList.SelectedItem.Text
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
Select a color from the list:<asp:DropDownList id="ddlColorList" runat="server"></asp:DropDownList>
</p>
<p>
Then select a font sytle from the list:
<asp:DropDownList id="ddlFontList" runat="server"></asp:DropDownList>
</p>
<p>
Finally, choose your output message:
<asp:DropDownList id="ddlQuoteList" runat="server"></asp:DropDownList>
</p>
<p>
<asp:Button id="btnSelectColor" onclick="btnSelectColor_Click" runat="server" Text="Click here!"></asp:Button>
</p>
<p>
<asp:Label id="lblOutputMessage" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
Use SortedList (C#)
<%@Page Language="c#" debug="true" %>
<script runat="server" Language="c#">
void Page_Load(object source, EventArgs e)
{
SortedList mySortedList = new SortedList();
mySortedList["a"]="aa";
mySortedList["b"]="bb";
mySortedList["c"]="cc";
mySortedList["d"]="dd";
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>