Asp.net之數組應用

來源:互聯網
上載者:User
asp.net|數組

string[] abc=new string[8]{"1","2","3","4","1","2","3","4"};
Response.Write(Array.IndexOf(abc,"3",1));//在abc數組中尋找"3",從abc[1]開始找
Response.Write(Array.LastIndexOf(abc,"3"));//在abc數組中尋找"3",從最後開始找
------------------------------------------------------------------------------
string[] arrStr=new string[8]{"1","4","3","2","16","14","12","14"};//arrStr[0]="1"...arrStr[7]="14"
Array.Reverse(arrStr); //顛倒arrStr數組,此時arrStr[0]="14"...arrStr[7]="1"
Array.Sort(arrStr); //給數組排序,此時順序為1,12,14,14,16,2,3,4(因為是按字串排序)
------------------------------------------------------------------------------
Array型數組要重定義大小,必須用ReDim(VB),對於大數組會特別慢;且無法在中間插入元素;不能清除它們(只能設定為空白或0)
ArrayList在使用上比Array慢,但是不用重定義大小,使用myArrList.Add("Dog")s可以方便的添加資料
ArrayList myArrList = new ArrayList();//不用指出數組的大小,而且每個元素可以是任意資料類型;
myArrList.Insert(1,"abc"); //插入元素到數組[1]前
myArrList.RemoveAt(1); //刪除數組元素[1]
myArrList.Remove("abc"); //刪除內容為"abc"的數組元素,只刪除一次,如果要全刪,需要做迴圈
------------------------------------------------------------------------------
ListItem newItem=new ListItem();newItem.Text="a";newItem.Value="b";
myDropDown.Items.Add(newItem);//使用ListItem為List框添加項
------------------------------------------------------------------------------
Hashtable ht =new Hashtable();ht["1"]="a";ht.Add("2","a");//Hashtable用法
SortedList sl=new SortedList();sl["1"]="a";sl.Add("2","a");//SortedList用法,會自動根據key進行排序
foreach(DictionaryEntry abc in sl) //遍曆SortedList的辦法
------------------------------------------------------------------------------
ConnStr=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\abc.mdb";//串連ACCESS資料庫,注意加了個@
ConnStr="Server=127.0.0.1;DataBase=MyDataBase;uid=username;pwd=password";//串連SQL Server資料庫
ConnStr="Provider=SQLOLEDB.1;Server=127.0.0.1;DataBase=MyDataBase;Initial Catalog=MyCatalog;uid=username;pwd=password";//用OLEDB串連SQL Server資料庫
------------------------------------------------------------------------------
ConnStr=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\abc.mdb";//串連ACCESS資料庫,注意加了個@
OleDbConnection con=new OleDbConnection(connstr);con.Open();con.Close();

OleDbConnection con=new OleDbConnection();
con.ConnectionString=ConnStr;con.Open();con.Close();
------------------------------------------------------------------------------
OleDbConnection con=new OleDbConnection(connstr);
OleDbCommand dc=new OleDbCommand("select * from employees",con);
OleDbDataReader dr=null;con.Open();dr=dc.ExecuteReader();
while(dr.Read()==true)Response.Write(dr["FirstName"]+"<BR>");
dr.Close();con.Close();//DataReader和Connection要關閉,Command不用
//這裡不用 dr=null, .Net會自動完成,不過加上dr=null會較快的釋放記憶體
------------------------------------------------------------------------------
<input type=text id="txtHtml" runat=server />

<asp:textbox id="txtAsp" runat=server />
的區別:
1.txtAsp使用OnClick,而txtHtml使用 OnServerClick
2.txtAsp使用Text來設定和擷取值,而txtHtml使用txtHtml.Value來設定和擷取值
3.<span id="sum" runat=server /> 使用InnerText屬性來寫入。
------------------------------------------------------------------------------
控制項:
<asp:listbox id="mylist" runat=server rows=6 selectionMode="Multiple">
<asp:ListItem Text="a" Selected="true" runat=server />
</asp:listbox>
ArrayList a = new ArrayList();for(int i=0;i<mylist.Items.Count;i++){if(mylist.Items[i].Selected)a.Add(i);}

<asp:DropDownList id="myddlist" runat=server>沒有 rows和selectionMode 屬性

<asp:CheckBoxList id="cblist" Runat=Server CellPadding=0 CellSpacing=0
RepeatColumns=3 RepeatDirection="Horizontal"或"Vertical">
<asp:ListItem Text="a" Selected="true" runat=server />
</asp:CheckBoxList>

<asp:RadioButtonList id="rblist" Runat=Server CellPadding=0 CellSpacing=0
RepeatColumns=3 RepeatDirection="Horizontal"或"Vertical">

list.SelectedItem.Text;list.SelectedIndex;list.Items[i].Selected;

list.Items.Insert( 0 , new ListItem ("全部" , "-1")) ;
把詳細資料add進去之後,最後執行下面兩句
select_info.Items.Add("choose one department");
select_info.SelectedIndex=select_kc.Items.Count-1;



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.