坚持学asp.net——(十二)
2024-07-10 13:07:29
供稿:网友
对象和结构化的数据
集合
作为集合的数组:下面是一个简单的例子:
<%@page language="c#" %>
<script runat="server" language="c#">
void page_load()
{
string[] animalarray = new string[5]
{ "dog", "cat", "elephant", "lion", "cat"};
mylabel.text = animalarray.getvalue(2).tostring()+"<font color=red> "+array.indexof(animalarray,"cat")+"</font>";
}
</script>
<html>
<asp:label id="mylabel" runat="server" />
</html>
这个程序输出显然是:elephant。
下面是一个对数组中某处遍历的程序:
<%@page language="c#" %>
<script runat="server" language="c#">
void page_load()
{
int intcounter = -1;
string[] animalarray = new string[5]
{ "dog", "cat", "elephant", "lion", "cat"};
do
{
intcounter = array.indexof(animalarray, "cat", intcounter+1);
mytext.innerhtml += "animalarray[" + intcounter + "]<br/>";
} while (intcounter != array.lastindexof(animalarray, "cat"));
}
</script>
<html>
the string "cat" occurs in the following elements:
<br/>
<div id="mytext" runat="server" />
</html>
颠倒数组中元素的顺序:array.reverse(array1);
将元素排序:array.sort(array1);
使用数组:
<%@page language="c#" %>
<script runat="server" language="c#">
void page_load()
{
string[] animalarray = new string[5]
{ "dog", "cat", "elephant", "lion", "cat" };
array.reverse(animalarray);
foreach (string stranimal in animalarray)
{
mydropdownlist.items.add(stranimal);
}
}
</script>
<html>
<form id="form1" method="post" runat="server">
<asp:dropdownlist id="mydropdownlist" runat="server" />
</form>
</html>
数据绑定:
集合的通用功能是:添加一对语句就可以将集合指定为数据源。
如前面的程序:
mydropdownlist.datasource=animalarray;
mydropdownlist.databind();
arraylist
定义:arraylins myarraylist=new arraylist();
每一个新项都会自动添加到公文的末尾!
前面的程序可以修改成这样:
<%@page language="c#" %>
<script runat="server" language="c#">
void page_load()
{
arraylist animalarraylist = new arraylist();
animalarraylist.add("dog");
animalarraylist.add("cat");
animalarraylist.add("elephant");
animalarraylist.add("lion");
animalarraylist.add("cat");
mydropdownlist.datasource = animalarraylist;
mydropdownlist.databind();
}
</script>
<html>
<form id="form1" method="post" runat="server">
<asp:dropdownlist id="mydropdownlist" runat="server" />
</form>
</html>
arraylist的一些方法:
添加:
myarraylist.add(“pig“);
插入:
myarraylist.insert(3,“long“);
删除:
myarraylist.removeat(3);
or
myarraylist.remove(“cat“);
hashtable
创建hashtable:hashtable myhashtable=new hashtable();
添加值的两种方式:
myhashtable.add([uk],“hongkong“);
or
myhashtable[uk]=“hongkong“;
hashtable.add() 采用两个参数,一个用于键,一个用于值。两者都属于类型对象。为键传递的值是整数,因此必须将其装箱以便作为对象进行传递。为值传递的值是字符串,它是引用类型,因此不对字符串进行装箱。每答对一个得一分。
e.g.:
<%@page language="c#" debug="true" %>
<script runat="server" language="c#">
void page_load(object source, eventargs e)
{
hashtable myhashtable = new hashtable();
myhashtable["uk"] = "united kingdom";
myhashtable["us"] = "united states";
myhashtable["de"] = "germany";
if (!(page.ispostback))
{
foreach (dictionaryentry item in myhashtable)
{
listitem newlistitem = new listitem();
newlistitem.text = item.value.tostring();
newlistitem.value = item.key.tostring();
mydropdownlist.items.add(newlistitem);
}
}
}
void click(object source, eventargs e)
{
mylabel.text = mydropdownlist.selecteditem.value;
}
</script>
<html>
<form runat="server">
<asp:dropdownlist id="mydropdownlist" runat="server" />
<asp:button id="mybutton" runat="server" text="ok" onclick="click" />
<br /><br />
<asp:label id="mylabel" runat="server" text="" />
</form>
</html>
sortedlist
类似hashtable,其中的值排序按照健值排序,而不是值!
使用:
<%@page language="c#" debug="true" %>
<script runat="server" language="c#">
void page_load(object source, eventargs e)
{
sortedlist mysortedlist = new sortedlist();
mysortedlist["armadillo"]="any of a family ... small bony plates";
mysortedlist["amaryllis"]="an autumn-flowering ... hippeastrum or sprekelia]";
mysortedlist["zebra"]="any of several fleet ... white or buff";
mysortedlist["artichoke"]="a tall composite herb ... cooked as a vegetable";
if (!(page.ispostback))
{
foreach (dictionaryentry item in mysortedlist)
{
listitem newlistitem = new listitem();
newlistitem.text = item.key.tostring();
newlistitem.value = item.value.tostring();
mydropdownlist.items.add(newlistitem);
}
}
}
void click(object source, eventargs e)
{
mylabel.text = mydropdownlist.selecteditem.value;
}
</script>
<html>
<form runat="server">
pick a word from the list:
<asp:dropdownlist id="mydropdownlist" runat="server" />
<asp:button id="mybutton" runat="server" text="ok" onclick="click" />
<br /><br />
<b>definition: </b>
<asp:label id="mylabel" runat="server" text="" />
</form>
</html>
(380)