ASP.NET - SortedList ¶ÔÏó

SortedList ¶ÔÏó¼æÓÐ ArrayList ºÍ Hashtable ¶ÔÏóµÄÌØÐÔ¡£

SortedList ¶ÔÏó

SortedList ¶ÔÏó°üº¬Óüü/Öµ¶Ô±íʾµÄÏîÄ¿¡£SortedList ¶ÔÏó¿É°´ÕÕ×Ö·û˳Ðò»òÊý×Ö˳Ðò×Ô¶¯µØ¶ÔÏîÄ¿½øÐÐÅÅÐò¡£

ͨ¹ý Add() ·½·¨Ïò SortedList Ìí¼ÓÏîÄ¿¡£SortedList ¿Éͨ¹ý TrimToSize() ·½·¨µ÷ÕûΪ×îÖճߴ硣

ÏÂÃæµÄ´úÂë´´½¨ÁËÒ»¸öÃûΪ mycountries µÄ SortedList£¬²¢Ìí¼ÓÁËËĸöÔªËØ£º

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New SortedList
  mycountries.Add("C","China")
  mycountries.Add("S","Sweden")
  mycountries.Add("F","France")
  mycountries.Add("I","Italy")
end if
end sub
</script>

Êý¾Ý°ó¶¨

SortedList ¶ÔÏó¿É×Ô¶¯µØÎªÏÂÃæµÄ¿Ø¼þÉú³ÉÎı¾ºÍÖµ£º

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox

ÈçÐè°ÑÊý¾Ý°ó¶¨µ½ RadioButtonList ¿Ø¼þ£¬Ê×ÏÈÇëÔÚ aspx ÎļþÖд´½¨Ò»¸ö RadioButtonList ¿Ø¼þ£¨Ã»ÓÐÈκΠasp:ListItem ÔªËØ£©£º

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>

</body>
</html>

È»ºóÌí¼Ó¹¹½¨ÁбíµÄ½Å±¾£º

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New SortedList
  mycountries.Add("C","China")
  mycountries.Add("S","Sweden")
  mycountries.Add("F","France")
  mycountries.Add("I","Italy")
  rb.DataSource=mycountries
  rb.DataValueField="Key"
  rb.DataTextField="Value"
  rb.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>

</body>
</html>

È»ºóÎÒÃÇÌí¼ÓÒ»¸ö×ÓÀý³Ì£¬¸Ã×ÓÀý³Ì»áÔÚÓû§µã»÷ RadioButtonList ¿Ø¼þÖеÄÏîĿʱִÐС£µ±µ¥Ñ¡°´Å¥±»µã»÷ʱ£¬Îı¾½«³öÏÖÔÚ label ÖУº

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New SortedList
  mycountries.Add("C","China")
  mycountries.Add("S","Sweden")
  mycountries.Add("F","France")
  mycountries.Add("I","Italy")
  rb.DataSource=mycountries
  rb.DataValueField="Key"
  rb.DataTextField="Value"
  rb.DataBind()
end if
end sub

sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & rb.SelectedItem.Text
end sub
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

ÏÔʾÕâ¸öÀý×Ó