1.<%=...%>與<%#... %>的區(qū)別:
答:<%=...%>是在程序執(zhí)行時調(diào)用,<%#... %>是在databind()方法之后被調(diào)用
2.控件接收哪些類型數(shù)據(jù)?
答:接收bind的控件,一般有dropdownlist,datalist,datagrid,listbox這些集合性質(zhì)的控件,而被捆綁 的主要是arraylist(數(shù)組),hashtable(哈稀表),dataview(數(shù)據(jù)視圖),datareader這四個,以后我們就可以 對號入座,不會出現(xiàn)datatable被捆綁的錯誤了:)
3.databind,獲得的數(shù)據(jù),系統(tǒng)會將其默認(rèn)為string,怎樣轉(zhuǎn)化為其它的類型?
databinder.eval(container.dataitem,"轉(zhuǎn)換的類型","格式")
最后一個"格式"是可選的,一般不用去管他,container.dataitem是捆綁的數(shù)據(jù)項,"轉(zhuǎn)換類型"指的是 integer,string,boolean這一類東西.
4.主要命名空間:
<% @ import namespace="system.data" %> 處理數(shù)據(jù)時用到
<% @ import namespace="system.data.ado" % > 使用ado.net ; 時用到
<% @ import namespace="system.data.sql" %> sql server 數(shù)據(jù)庫專用
<% @ import namespace="system.data.xml" %> 不用看處理xml用到
<% @ import namespace="system.io" %> 處理文件時用到
<% @ import namespace="system.web.util" %> 發(fā)郵件時大家會用到
<% @ import namespace="system.text" %> 文本編碼時用到
5.connections(sqlconection 或者 adoconnection)的常用屬性和方法:
| connectionstring 取得或設(shè)置連結(jié)數(shù)據(jù)庫的語句
| connectiontimeout 取得或設(shè)置連結(jié)數(shù)據(jù)庫的最長時間,也是就超時時間
| database 取得或設(shè)置在數(shù)據(jù)庫服務(wù)器上要打開的數(shù)據(jù)庫名
| datasource 取得或設(shè)置dsn,大家不會陌生吧:)
| password 取得或設(shè)置密碼
| userid 取得或設(shè)置登陸名
| state 取得目前聯(lián)結(jié)的狀態(tài)
| open() 打開聯(lián)結(jié)
| close() 關(guān)閉聯(lián)結(jié)
| clone() 克隆一個聯(lián)結(jié)。(呵呵,綿羊可以connection我也可以)
示例:
sqlconnection myconnection = new sqlconnection();
myconnection.datasource = "mysqlserver";
myconnection.password = "";
myconnection.userid = "sa";
myconnection.connectiontimeout = 30;
myconnection.open();
myconnection.database = "northwind";
myconnection.isolationlevel = isolationlevel.readcommitted
6.command常用的方法和屬性
| activeconnection 取得或設(shè)置聯(lián)結(jié)connections
| commandtext 執(zhí)行的sql語句或儲存過程(storedprocedure)名
| commandtimeout 執(zhí)行的最長時間
| commandtype command操作的類型(storedprocedure,text,tabledirect)三種,默認(rèn)text
| parameters 操作儲存過程時使用
| execute() 執(zhí)行sql語句或儲存過程
| executenonquery() 同上,區(qū)別在于不返回記錄集
| clone() 克隆command
示例:
string myselectquery = "select * from categories order by categoryid";
stringmyconnectstring="userid=sa;password=;database=northwind;server=mysqlserver";
sqlcommand mycommand = new sqlcommand(myselectquery);
mycommand.activeconnection = new sqlconnection(myconnectstring);
mycommand.commandtimeout = 15;
mycommand.commandtype = commandtype.text;< /font >
7.打開和關(guān)閉數(shù)據(jù)庫兩種方法:
1.myconnection.open(); //打開聯(lián)結(jié)
myconnection.close();
2.mycommand.activeconnection.open();
mycommand.activeconnection.close()
8.使用dataset,在數(shù)據(jù)庫中增加、修改、刪除一個數(shù)據(jù)
a.添加數(shù)據(jù)
datarow dr=mydataset.tables["userlist"].newrow();
dr["username"] = "周訊";
dr["remark"] = "100";
dr["comment"] = "漂亮mm";
mydataset.tables.rows.add(dr);
b.修改數(shù)據(jù)
mydataset.tables["userlist"].rows[0]["username"]="飛刀大哥";
c.刪除數(shù)據(jù)
mydataset.tables["userlist"],rows[0].delete();
d.恢復(fù)數(shù)據(jù)
if(mydataset.haserrors)
{
mydataset.rejectchanges();
}
e.探測dataset是否有改動
if(mydataset.haschanges)
{
//保存代碼
}else{
//因為沒有變化,所以不用保存,以節(jié)省時間
}
f.更新數(shù)據(jù)庫
mycomm.update(mydataset); //更新數(shù)據(jù)庫中所有的表
mycomm.update(mydataset,"userlist"); //更新某個表
9.datagrid實現(xiàn)分頁功能
allowpaging="true" //是指允許分頁,這個是最主要的。有了它,我們才能分頁。
pagesize="5" //是指定每頁顯示的記錄數(shù),如果不寫,就會默認(rèn)為10條。
pagerstyle-horizontalalign="right" //是指定分面顯示的定位,默認(rèn)是left
pagerstyle-nextpagetext="下一頁" //把<>改為上一頁和下一頁字符串
pagerstyle-prevpagetext="上一頁"
pagerstyle-mode="numericpages" //把<>改為123數(shù)字顯示
10.顯示一共有多少頁,并且報告當(dāng)前為第幾頁
當(dāng)前頁是:<font color=red><%=datagrid1.currentpageindex+1%></font><br>
總頁數(shù)是:<font color=red><%=datagrid1.pagecount%></font><br>
11.個性化分頁
程序員大本營之"親密接觸asp.net(14)"有完整代碼
12.要將頁面重置為有效的狀態(tài)
ivalidator val;
foreach(val in validators)
{
val.isvalid = true;
}
13.重新執(zhí)行整個驗證序列
ivalidator val;
foreach(val in validators)
{
val.validate();
}
14.禁用客戶端驗證
<%@ page language="c#" clienttarget=downlevel %>
15.repeater、datalist和datagrid控件用途"
這些控件可以簡化幾種常見的 web 應(yīng)用程序方案,包括報表、購物車、產(chǎn)品列表、查詢
結(jié)果和導(dǎo)航菜單。 repeater是唯一允許在其模板中存在 html片段的控件.
16.server.execute("another.aspx")和server.transfer("another.aspx")區(qū)別:
execute是從當(dāng)前頁面轉(zhuǎn)移到指定頁面,并將執(zhí)行返回到當(dāng)前頁面
transfer是將執(zhí)行完全轉(zhuǎn)移到指定頁面
17.xml文件中可以自己存有架構(gòu),也可以存在于*.xsl文件中,但必須通過xmlns屬性在xml文檔的根節(jié)點中指定該信息,如下所示:
<rootelement xmlns="x-schema:scheduledschema.xsl">
18.xml文件的讀取
filestream myfs=new filestream(server.mappath("xmldtagrid.xml"),filemode.open,fileaccess.read);
streamreader myreader=new streamreader(myfs);
dataset myds=new dataset();
myds.readxml(myreader);
19.正則表達(dá)式 控件regularexpressionvalidator
符號 含義
^ 指定檢查開始處
$ 指定檢查結(jié)束處
[] 檢查輸入的值是否與方括弧中的字符之一相匹配
/w 允許輸入任何值
/d{} "/d"指定輸入的值是一個數(shù)字,{}表示已指定數(shù)據(jù)類型的出現(xiàn)次數(shù)
+ 表明一個或多個元素將被添加到正在檢查的表達(dá)式
示例:電子郵件格式(具有@號,且以.com/.net/.org/.edu結(jié)尾)
validationexpression="^[/w-][email protected][/w-]+/.(com|net|org|edu)$"
20.datagrid控件中數(shù)據(jù)操作重要語句:
屬性:datakeyfield="userid" //設(shè)userid為表的主鍵,無法將該字段的值更新到數(shù)據(jù)庫,最好設(shè)表的主鍵為datagrid的主鍵
sqlcommand.parameters["@userid"].value=dg.datakeys[(int)e.item.itemindex]; //檢索所要更新的行的主鍵(將當(dāng)前選定的行的 主鍵值賦給命令的一個參)數(shù)
sqlcommand.parameters["@fname"].value=((textbox)e.item.cells[2].controls[0]).text; //為參數(shù)賦予已修改的行值
21.自定義控件:
a.用戶控件(asp創(chuàng)建頁面一樣)
(i). 創(chuàng)建頁面,拖入控件,設(shè)置屬性/方法. <% @control language="c#" debug="true" %>中的@control指令來定義此頁 將包含控件代碼
(ii) 保存為*.ascx文件,如a.ascx.
(iii).使用: 頭<%@register tagprefix="myfirstcontrol" tagname="mylbl" src="a.axcs" %>
//tagprefix為控件的前綴,像asp:textbox中的asp
//tagname用于指定自定義控件的名稱
//src指定控件文件源
身體:<myfirstcontrol:mylbl runat="server" id="allmine" mytext="成功了" />
b.使用c#創(chuàng)建自定義控件
(i). 創(chuàng)建純代碼文件,繼承基類control,并保存為*.cs,如a.cs.
(ii).將代碼編譯生成程序集: csc /t:library /r:system.dll,system.web.dll a.cs
//library告訴c#編譯器生成程序集
// /r:system.dll system.web.dll告訴c#編譯器引用指定的程序集
(iii).將生成dll文件放在bin目錄中
(iv).使用: <% @register tagprefix="mine" namespace="myowncontrols" assembly="a" %>
22.復(fù)合控件注意事項:
public class mycompositin:control,inamingcontainer //inamingcontainer:如果在頁面上有多個此控件實例,則此結(jié)口可以給每 {} //個實例有唯一標(biāo)志
this.ensurechildcontrols();//表示將復(fù)合控件的子控件都呈現(xiàn)到頁面上,此方法檢查服務(wù)器控件是否包含子控件
createchildcontrols
23.button/linkbutton/imagebutton/hyperlink什么時候用?
1.button和imagebutton用于將數(shù)據(jù)傳遞回服務(wù)器.
2.hyperlink用于在頁面之間導(dǎo)航
3.linkbutton用于將數(shù)據(jù)保存到服務(wù)器或訪問服務(wù)器上的數(shù)據(jù)
24.跟蹤調(diào)試
跟蹤:
1.頁級別跟蹤: 在頁的開頭包括如下的頁指令<%@ page trace="true" tracemode="sortbycategory/sortbytime" %>
自定義消息:
trace.write("這里為要顯示的字符串");
trace.warn("這里為要顯示的字符串"); //與trace.write相同,只是字體為紅色
檢查是否使用了跟蹤
例句: if(trace.isenabled) { trace.warn("已啟用跟蹤")}
2.應(yīng)用程序級別跟蹤: 在web.config文件的<system.web>節(jié)中 <trace enabled="true" pageoutput="true"/>
25.設(shè)置緩存:
1.輸出緩存:
i.頁面設(shè)置: 將 <%@ outputcache duration="120" varybyparam="none" %> 加在需要緩存頁的開頭
注釋:在請求該頁的后兩分鐘之內(nèi),輸出內(nèi)容不變
ii.編程方式設(shè)置:
主要使用類system.web.httpcachepolicy類下的方法
(1). response.cache.setexpires(datetime.now.addseconds(120)); //在此方法中必須指定到期時間,如本語 //句為兩分鐘
(2). response.cache.setexpires(datetime.now.addseconds(120));
response.cache.setslidingexpiration(true); //"可調(diào)到期",主要用于那些開始訪問量大,但隨后訪問 //量平衡的情況
功能:第一句設(shè)置緩存到期時間,第二行打開 sliding expiration(可調(diào)到期).
2.數(shù)據(jù)緩存:
(1).dataview mysource; (2).給mysource賦值;
(3).cache["mycache"]=mysource; (4).mysource=(dataview)cache["mycache"]
26.部署: 直接復(fù)制到產(chǎn)品服務(wù)器即可 復(fù)制語句: xcopy <source_path> <destination_path> //xopy只接受物理路徑,不接受虛擬路徑
新聞熱點
疑難解答
圖片精選