堅持學(xué)asp.net——(十二)
2024-07-10 13:07:29
供稿:網(wǎng)友
 
對象和結(jié)構(gòu)化的數(shù)據(jù)
集合
作為集合的數(shù)組:下面是一個簡單的例子:
<%@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。
下面是一個對數(shù)組中某處遍歷的程序:
<%@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>
顛倒數(shù)組中元素的順序:array.reverse(array1); 
將元素排序:array.sort(array1); 
使用數(shù)組:
<%@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>
 
數(shù)據(jù)綁定:
 集合的通用功能是:添加一對語句就可以將集合指定為數(shù)據(jù)源。
如前面的程序:
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,“l(fā)ong“);
刪除:
myarraylist.removeat(3);
or
myarraylist.remove(“cat“);
hashtable
創(chuàng)建hashtable:hashtable myhashtable=new hashtable(); 
添加值的兩種方式:
myhashtable.add([uk],“hongkong“);
 or
myhashtable[uk]=“hongkong“;
hashtable.add() 采用兩個參數(shù),一個用于鍵,一個用于值。兩者都屬于類型對象。為鍵傳遞的值是整數(shù),因此必須將其裝箱以便作為對象進(jìn)行傳遞。為值傳遞的值是字符串,它是引用類型,因此不對字符串進(jìn)行裝箱。每答對一個得一分。 
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)