您身边的网站建设专家
成功案例

【建站服务】喀什企业网站设计 - 喀什高端网站定制 - 喀什品牌网站搭建 - 上往建站-域名申请

日期: 2022-09-21 05:41:46 浏览数:4


上往建站提供服务器空间服务商百度快照排名网站托管百度推广运营,致力于设计外包服务与源代码定制开发360推广搜狗推广,增加网站的能见度及访问量提升网络营销的效果,主营:网站公司,百度推广公司电话,官网搭建服务,网站服务企业排名,服务器空间,英文域名等业务,专业团队服务,效果好。


喀什企业网站设计 - 喀什高端网站定制 - 喀什品牌网站搭建 - 上往建站

网站建设.png

ASP.NET Web Forms - Hashtable 对象


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


Examples

尝试一下 - 实例

Hashtable RadiobuttonList 1

Hashtable RadiobuttonList 2

Hashtable DropDownList


创建 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 对象可为下列的控件自动生成文本和值:

为了绑定数据到 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 控件中的某个项目时,该子例程会被执行。当某个单选按钮被点击时,label 中会出现一行文本:

实例

<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 对象。


ASP.NET Web Forms - SortedList 对象

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


Examples

尝试一下 - 实例

SortedList RadiobuttonList 1


SortedList RadiobuttonList 2


SortedList DropDownList


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 对象可为下列的控件自动生成文本和值:


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>

然后添加创建列表的脚本,并且绑定列表中的值到 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 控件中的某个项目时,该子例程会被执行。当某个单选按钮被点击时,label 中会出现一行文本:


实例

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


演示实例 »


喀什企业网站设计 - 喀什高端网站定制 - 喀什品牌网站搭建 - 上往建站


上往建站提供搭建网站域名注册官网备案服务网店详情页设计企业网店专业网络店铺管理运营全托管公司咨询电话,服务器空间,微信公众号托管网页美工排版,致力于域名申请竞价托管软文推广全网营销,提供标准级专业技术保障,了却后顾之忧,主营:虚拟主机网站推广百度竞价托管网站建设上网建站推广服务网络公司有哪些等业务,专业团队服务,效果好。

服务热线:400-111-6878 手机微信同号:18118153152(各城市商务人员可上门服务)


全国咨询热线:400-111-6878

地址:全国各地都有驻点商务

Copyright © 2021 通陆科技

网站建设上往建站