ASP.NET - The SortedList Object
2007-03-06 15:33
393 查看
1.Example 1 - SortedList 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>
Output Result:
|
when you click one of the radios ,it will shows:(example:you have clicked the radio 'france')
|
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim navigate=New SortedList
navigate.Add("RadioButtonList","control_radiobuttonlist.asp")
navigate.Add("CheckBoxList","control_checkboxlist.asp")
navigate.Add("DropDownList","control_dropdownlist.asp")
navigate.Add("ListBox","control_listbox.asp")
rb.DataSource=navigate
rb.DataValueField="Value"
rb.DataTextField="Key"
rb.DataBind()
end if
end sub
sub navigate(s as Object, e As EventArgs)
response.redirect(rb.SelectedItem.Value)
end sub
</script>
<html> <body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="navigate" />
</form>
</body> </html>
Output Result:
|
the hamepage will redirect to the url 'control_checkboxlist.asp'
3.Example 3 - SortedList DropDownList
<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")
dd.DataSource=mycountries
dd.DataValueField="Key"
dd.DataTextField="Value"
dd.DataBind()
end if
end sub
sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & dd.SelectedItem.Text
end sub
</script>
<html> <body>
<form runat="server">
<asp:DropDownList id="dd" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>
</body> </html>
Output Result:
France Italy Norway Sweden |
France Italy Norway Sweden Your favorite country is: France |
The SortedList Object
The SortedList object contains items in key/value pairs. A SortedList object automatically sort the items in alphabetic or numeric order.
Items are added to the SortedList with the Add() method. A SortedList can be sized to its final size with the TrimToSize() method.
The following code creates a SortedList named mycountries and four elements are added:
<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> |
Data Binding
A SortedList object may automatically generate the text and values to the following controls:asp:RadioButtonList
asp:CheckBoxList
asp:DropDownList
asp:Listbox
To bind data to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page:
<html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" /> </form> </body> </html> |
<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> |
<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> |
相关文章推荐
- ASP.NET - The Hashtable Object
- 解决asp.net error: Operation is not valid due to the current state of the object
- 【ASP.NET Step by Step】之六 Programmatically Setting the ObjectDataSource's Parameter Values
- ASP.NET - The ArrayList Object
- ASP.NET4.0错误:Operation is not valid due to the current state of the object
- The ASP.NET Object Model
- 【ASP.NET Step by Step】之四 Displaying Data With the ObjectDataSource
- Accessing the GAL(Global Address List) from ASP.NET
- Understanding the Session Object in ASP.NET 2.0
- ASP.NET DEMO 12 : CheckBoxList 实现单选【转】
- asp.net set theme
- asp.net将object或string转为int
- ASP.NET中Dictionary如何转换为list
- ASP.NET CheckBoxList Operations with jQuery
- Asp.net 中CheckBoxList 的应用
- ASP .NET Maintaining the ViewState
- Server-side ASP .NET Data Binding, Part 2: Customizing the DataGrid Control
- asp.net CheckBoxList 取值与是否选中,后台控制前台checkboxList选中
- 技术笔记3_Asp.net:ObjectDataSource分页计算返回数据量的方法QueryStudentsCount必须返回int
- Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application