ASP.NET 常用技巧學習

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 =@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 調研存儲過程例子

<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 作爲數據源
支持緩存與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 列類型:
6. 衝突判定

 

名稱                                             描述
BoundField                                 顯示數據庫中取出的文本
ButtonField                                顯示按鈕
CheckBoxField                           使用check boxes顯示一個boolean型變量
HyperLinkField                          顯示一個超鏈接
TemplateField                            顯示一個自定義的HTML模板
CommandField                          顯示一個查詢或者編輯按鈕
ImageField                                 顯示一個圖片

 

先入勝利
如果數據在取出之後被改變,則修改失敗
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 lastname=@lastname, firstname=
@firstnamewhere employeeid=@original_employeeid and lastname=
@original_lastnameand firstname=@original_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
}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章