1.簡單數(shù)據(jù)綁定定
<!--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.數(shù)據(jù)源控件
控件名 控件描述
SqlDataSource 一切支持SQL語句的數(shù)據(jù)源控件
AccessDataSource Access數(shù)據(jù)源控件
XmlDataSource XML數(shù)據(jù)源控件
ObjectDataSource 自行編寫組件的數(shù)據(jù)源控件
SiteMapDataSource 頁面導(dǎo)航控件的數(shù)據(jù)源控件
2.1 SqlDataSource關(guān)鍵屬性
名稱 描述
ConnectionString 連接數(shù)據(jù)庫的連接字符串
SelectCommand 用于執(zhí)行查詢的命令
InsertCommand 用于執(zhí)行插入的命令
UpdateCommand 用于執(zhí)行更新的命令
DeleteCommand 用于執(zhí)行刪除的命令
DataSourceMode 指定數(shù)據(jù)源類型是DataSet 或DataReader( 默認(rèn)值= DataSet)
ProviderName 指定供應(yīng)商(默認(rèn)值= SQL Server .NET provider)
2.2 SqlDataSource 支持通過以下屬性進(jìn)行數(shù)據(jù)緩存
屬性名 描述
EnableCaching 指定是否打開緩存(默認(rèn)值= false)
CacheDuration 指定結(jié)果被緩存多少妙
CacheExpirationPolicy 指定緩存間隔是sliding 還是absolute
CacheKeyDependency 使緩存依賴于一個(gè)特定鍵值
SqlCacheDependency 使緩存依賴于一個(gè)特定數(shù)據(jù)庫實(shí)體
2.3 參數(shù)化命令 XxxParameter 類型指定參數(shù)來源
名稱 描述
SelectParameters 為查詢命令指定參數(shù)
InsertParameters 為插入命令指定參數(shù)
UpdateParameters 為更新命令指定參數(shù)
DeleteParameters 為刪除命令指定參數(shù)
FilterParameters 為過濾器命令指定參數(shù)
2.4 XxxParameter 類型
名稱 描述
ControlParameter 指定一個(gè)源自于控件的參數(shù)
CookieParameter 指定一個(gè)源自于cookie的參數(shù)
FormParameter 指定一個(gè)源自于表單的參數(shù)
ProfileParameter 指定一個(gè)源自于profile的參數(shù)
QueryStringParameter 制定于一個(gè)來源于查詢字符串的參數(shù)
Parameter 為數(shù)據(jù)源綁定一個(gè)參數(shù)
SessionParameter 指定一個(gè)源自于session的參數(shù)
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 =@Country">
<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 調(diào)研存儲過程例子
<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
GO
3.XmlDataSource 使用XML 作為數(shù)據(jù)源
支持緩存與XSL 轉(zhuǎn)換,只支持查詢綁定,不支持更新
<asp:XmlDataSourceID="Rates" DataFile="Rates.xml" RunAt="server" />
<asp:TreeViewID="MyTreeView" DataSourceID="Rates" RunAt="server" />
3.1 XmlDataSource的關(guān)鍵屬性
名稱 描述
DataFile XML 數(shù)據(jù)文件的路徑
TransformFile 含有XSL 風(fēng)格定義的數(shù)據(jù)文件路徑
EnableCaching 指定是否開啟cache (默認(rèn)值= false)<