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

ASP.NET Web Forms – Hashtable 对象简介

33次阅读
没有评论

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

导读 Hashtable 对象包含用键 / 值对表示的项目。

ASP.NET Web Forms – Hashtable 对象简介

创建哈希表

Hashtable 包含用键对表示的项目。键被快速索引,通过搜索键,可以实现对值的搜索。

通过 Add() 方法向 Hashtable 添加项目。

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

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

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

  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 Hashtable   
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 Hashtable
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>

注释:您无法选择添加到 Hashtable 的项目的示例方式。如需对项目进行示例或数字示例,请使用 SortedList。

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

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

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

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