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控件。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。