精美而实用的网站,关注web编程技术、网站运营、SEO推广,让您轻松愉快的学习

ArrayList对象在ASP.NET编程中用途广泛,现在介绍下将数据绑定到ArrayList,ArrayList对象对以下控件可以自动生成文本和值。

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

要绑定数据到RadioButtonList控件,首先需要在.aspx页面内创建一个RadioButtonList控件(不带有任何asp:ListItem元素):

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

然后添加脚本,建立序列并在把序列中的值绑定到RadioButtonList控件:

<html>
<body>
<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New ArrayList
mycountries.Add("Norway")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")
mycountries.TrimToSize()
mycountries.Sort()
rb.DataSource=mycountries
rb.DataBind()
end if
end sub
</script>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>

请大家注意:数据值被同时应用于控件的Text和Value属性。要想赋值给Value的值与Text不同,使用Hashtable对象或是SortedList对象。RadioButtonList控件的DataSource属性被设置到ArrayList,它定义了RadioButtonList控件的数据源。RadioButtonList控件的DataBind()方法绑定数据源和RadioButtonList控件。

Tags:ASP.NET ArrayList