阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

ASP.NET Web Forms – SortedList 对象简介

41次阅读
没有评论

共计 1889 个字符,预计需要花费 5 分钟才能阅读完成。

导读 SortedList 对象结合了 ArrayList 对象和 Hashtable 对象的特性。

ASP.NET Web Forms – SortedList 对象简介

SortedList 对象

SortedList 对象包含用键对对象的项目自动表示。按顺序对对象或数字顺序地项目进行排序。

通过 Add() 方法向 SortedList 添加项目。通过 TrimToSize() 方法把 SortedList 调整为最终尺寸。

下面的代码创建了一个名为 mycountries 的 SortedList 对象,并添加了四个元素:

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then   
dim mycountries=New SortedList   
mycountries.Add("N","Norway")  
mycountries.Add("S","Sweden")  
mycountries.Add("F","France")   
mycountries.Add("I","Italy")
end if
end sub
</script>
数据绑定

SortedList 可以后续的控件自动生成文本和值:

  1. asp: 单选按钮列表
  2. asp: 复选框列表
  3. asp: 下拉列表
  4. asp: 列表框

为了将数据绑定到 RadioButtonList 控件,首先要在 .aspx 页面中创建一个 RadioButtonList 控件(不带 asp:ListItem 元素):

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

然后添加创建列表的脚本,然后将绑定列表中的值添加到 RadioButtonList 控件中:

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then   
dim mycountries=New SortedList 
mycountries.Add("N","Norway")   
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 时,会在该控件中添加一个程序的项目时,执行。当单选按钮被点击时,中会行的标签文本:

实例

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New SortedList
mycountries.Add("N","Norway")
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>

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计1889字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中