1.簡單數據綁定定
<!--asp.net 1.x data binding expression -->
<%# databinder.eval(container.dataitem, "price") %>
<!--equivalent asp.net 2.0 data binding expression -->
<%# eval("price") %>
<!--xml data binding -->
<%# xpath("price") %>2.數據源控件
控件名 控件描述
sqldatasource 一切支持sql語句的數據源控件
accessdatasource access數據源控件
xmldatasource xml數據源控件
objectdatasource 自行編寫組件的數據源控件
sitemapdatasource 頁面導航控件的數據源控件 2.1 sqldatasource關鍵屬性
名稱 描述
connectionstring 連接數據庫的連接字符串
selectcommand 用于執行查詢的命令
insertcommand 用于執行插入的命令
updatecommand 用于執行更新的命令
deletecommand 用于執行刪除的命令
datasourcemode 指定數據源類型是dataset 或datareader( 默認值= dataset)
providername 指定供應商(默認值= sql server .net provider) 2.2 sqldatasource 支持通過以下屬性進行數據緩存
屬性名 描述
enablecaching 指定是否打開緩存(默認值= false)
cacheduration 指定結果被緩存多少妙
cacheexpirationpolicy 指定緩存間隔是sliding 還是absolute
cachekeydependency 使緩存依賴于一個特定鍵值
sqlcachedependency 使緩存依賴于一個特定數據庫實體2.3 參數化命令 xxxparameter 類型指定參數來源
名稱 描述
selectparameters 為查詢命令指定參數
insertparameters 為插入命令指定參數
updateparameters 為更新命令指定參數
deleteparameters 為刪除命令指定參數
filterparameters 為過濾器命令指定參數2.4 xxxparameter 類型
名稱 描述
controlparameter 指定一個源自于控件的參數
cookieparameter 指定一個源自于cookie的參數
formparameter 指定一個源自于表單的參數
profileparameter 指定一個源自于profile的參數
querystringparameter 制定于一個來源于查詢字符串的參數
parameter 為數據源綁定一個參數
sessionparameter 指定一個源自于session的參數2.5 使用controlparameter例子 <asp:sqldatasourceid="countries" runat="server"
connectionstring="server=localhost;database=northwind;"
selectcommand="select distinct country from customers order by country" />
<asp:sqldatasourceid="customers" runat="server"
connectionstring="server=localhost;database=northwind;"
selectcommand="select * from customers where country [email protected]">
<selectparameters>
<asp:controlparametername="country" controlid="mydropdownlist"
propertyname="selectedvalue" />
</selectparameters>
</asp:sqldatasource>
<asp:dropdownlistid=" mydropdownlist" datasourceid="countries"
datatextfield="country" autopostback="true" runat="server" />
<asp:datagriddatasourceid="customers" runat="server" />2.7 調研存儲過程例子
<asp:sqldatasourceid="countries" runat="server"
connectionstring="server=localhost;database=northwind;"
selectcommand="proc_getcountries" />
<asp:sqldatasourceid="customers" runat="server"
connectionstring="server=localhost;database=northwind;"
selectcommand="proc_getcustomers">
<selectparameters>
<asp:controlparametername="country" controlid="mydropdownlist"
propertyname="selectedvalue" />
</selectparameters>
</asp:sqldatasource>
<asp:dropdownlistid="mydropdownlist" datasourceid="countries"
datatextfield="country" autopostback="true" runat="server" />
<asp:datagriddatasourceid="customers" runat="server" />
create procedure proc_getcustomers
@country nvarchar(32) as
select * from customers
where country = @country
go
create procedure proc_getcustomers
create procedure proc_getcountriesas
select distinct country
from customers
order by country
go3.xmldatasource 使用xml 作為數據源
支持緩存與xsl 轉換,只支持查詢綁定,不支持更新
<asp:xmldatasourceid="rates" datafile="rates.xml" runat="server" />
<asp:treeviewid="mytreeview" datasourceid="rates" runat="server" />3.1 xmldatasource的關鍵屬性
名稱 描述
datafile xml 數據文件的路徑
transformfile 含有xsl 風格定義的數據文件路徑
enablecaching 指定是否開啟cache (默認值= false)
xpath xpath 表達式用來確認數據
cacheduration 以秒為單位的時間間隔
cacheexpirationpolicy 指定時間間隔是sliding 還是absolute
cachekeydependency 創建緩存依存于某個鍵
4. objectdatasource
從數據組件綁定數據,提供中間件的數據綁定,使數據訪問和ui脫離,兩種數據綁定方式
selectmethod, insertmethod, updatemethod,and deletemethod
可選擇是否使用緩存,可選擇是否使用參數
4.1 ods 關鍵屬性
ods 關鍵屬性
名稱 描述
insertparameters 指定插入方法參數
updateparameters 指定更新方法參數
deleteparameters 指定刪除方法參數
selectparameters 指定查詢方法參數
cacheduration 緩存間隔時間以秒為單位
sqlcachedependency 基于某個數據實體的緩存
創建和清除
objectdatasource.selectmethod 可以使用靜態方法也可以使用一個類的新實例
如果使用實例方法:ods 在每次調用的時候創建一個新實例類必須具有公共的建構函數
使用objectcreated 和objectdisposing 元素可以初始化和撤銷函數5.增強的datagrid 控件
支持復雜的數據單元格類型,包括checkboxfields在<columns> 元素中聲明高可定制的用戶界面
gridview 列類型:
名稱 描述
boundfield 顯示數據庫中取出的文本
buttonfield 顯示按鈕
checkboxfield 使用check boxes顯示一個boolean型變量
hyperlinkfield 顯示一個超鏈接
templatefield 顯示一個自定義的html模板
commandfield 顯示一個查詢或者編輯按鈕
imagefield 顯示一個圖片6. 沖突判定
先入勝利
如果數據在取出之后被改變,則修改失敗
updatecommand結構構成指定conflictdetection=“compareallvalues”來實現
后入勝利
無論數據是否被修改,該修改都會成功
updatecommand結構構成指定conflictdetection=“overwritechanges”來實現6.1 先入勝利法則更新
<asp:sqldatasourceid="employees" runat="server"
connectionstring="server=localhost;database=northwind;"
selectcommand="select employeeid, lastname, firstnamefrom employees"
updatecommand="update employees set [email protected], firstname=
@firstnamewhere [email protected]_employeeid and lastname=
@original_lastnameand [email protected]_firstname"
conflictdetection="compareallvalues">
<updateparameters>
<asp:parametername="employeeid" type="int32" />
<asp:parametername="lastname" type="string" />
<asp:parametername="firstname" type="string" />
</updateparameters>
</asp:sqldatasource>
<asp:gridviewdatasourceid="employees" width="100%" runat="server"
datakeynames="employeeid" autogenerateeditbutton="true" />7.錯誤檢測
數據更新后控件調用的事件gridview.rowupdated,detailsview.itemupdated,sqldatasource.updated, etc.
處理“status”的事件,無論數據庫是否異常允許數據庫異常被處理或者再次拋棄,顯示多少數據庫行被修改
處理更新錯誤
<asp:sqldatasourceid="employees" runat="server"
updatecommand="" onupdated="onupdatecomplete">
</asp:sqldatasource>
void onupdatecomplete (object source, sqldatasourcestatuseventsargse)
{
if (e.exception!= null) {
// exception thrown. set e.exceptionhandledto true to prevent
// the sqldatasourcefrom throwing an exception, or leave it set
// to false to allow sqldatasourceto rethrowthe exception
}
else if (e.affectedrows== 0) {
// no exception was thrown, but no records were updated,either.
// might want to let the user know that the update failed
}
}
新聞熱點
疑難解答
圖片精選