📄 aspnet_arraylist.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ASP.NET ArrayList</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>ASP.NET - The ArrayList Object</h1>
<a href="aspnet_databinding.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" /></a>
<a href="aspnet_hashtable.asp"><img alt="next" border="0" src="../images/btn_next.gif" /></a>
<hr />
<p class="intro">The ArrayList object is a collection of items containing a
single data value.</p>
<hr />
<h2>Examples</h2>
<p><a target="_blank" href="showasp.asp@filename=demo_arraylist_radio1">Example 1 - ArrayList RadioButtonList</a></p>
<p><a target="_blank" href="showasp.asp@filename=demo_arraylist_drop1">Example 2 - ArrayList DropDownList</a></p>
<hr />
<h2>Create an ArrayList</h2>
<p>The ArrayList object is a collection of items containing a single data value.</p>
<p>Items are added to the ArrayList with the Add() method.</p>
<p>The following code creates a new ArrayList object named
mycountries and four items are added:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><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")
end if
end sub
</script></pre>
</td></tr>
</table>
<p>By default, an ArrayList object contains 16 entries. An ArrayList
can be sized to its final size with the TrimToSize() method:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><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()
end if
end sub
</script></pre>
</td></tr>
</table>
<p>An ArrayList can also be sorted alphabetically or numerically with the Sort()
method:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><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()
end if
end sub
</script></pre>
</td></tr>
</table>
<p>To sort in reverse order, apply the Reverse() method
after the Sort() method:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><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()
mycountries.Reverse()
end if
end sub
</script></pre>
</td></tr>
</table>
<br />
<hr />
<h2>Data Binding to an ArrayList</h2>
<p>An ArrayList object may automatically generate the text and values to the following controls:</p>
<ul>
<li>asp:RadioButtonList</li>
<li>asp:CheckBoxList</li>
<li>asp:DropDownList</li>
<li>asp:Listbox</li>
</ul>
<p>To bind data to a RadioButtonList control, first create a RadioButtonList control (without
any asp:ListItem elements) in an .aspx page:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><html>
<body></pre>
<pre><form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form></pre>
<pre></body>
</html></pre>
</td></tr>
</table>
<p>Then add the script that builds the list and binds the values
in the list to the RadioButtonList control:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><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></pre>
<pre><html>
<body></pre>
<pre><form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form></pre>
<pre></body>
</html></pre>
</td></tr>
</table>
<p>The DataSource
property of the RadioButtonList control is set to the ArrayList and it defines the
data source of the
RadioButtonList control. The DataBind() method of the RadioButtonList control binds the data source with
the RadioButtonList control.</p>
<p><b>Note:</b> The data values are used as both the Text
and Value properties for the control. To add Values that are different from the Text, use either
the Hashtable object or the SortedList object.</p>
<hr />
<a href="aspnet_databinding.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" /></a>
<a href="aspnet_hashtable.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -