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;