使用GridView來編輯和刪除數(shù)據(jù)之所以很簡單,是因為GridView和ObjectDataSource在底層非常一致。當更新按鈕被點擊時,GridView自動將字段的值賦給ObjectDataSource的UpdateParameters集合,然后激發(fā)ObjectDataSource的Update()方法。而DataList與Repeater并沒有相關使用方法。
需要確保將合適的值賦給ObjectDataSource的參數(shù),然后調用Update()方法。DataList提供了以下的屬性和事件來完成這些:
DataKeyField property — 更新或刪除時,需要唯一確定DataList里的每個item。將這個屬性設為顯示的數(shù)據(jù)的主健。這樣做會產生DataList的 DataKeys collection ,每個item都有一個指定的 DataKeyField .
EditCommand event — 當CommandName屬性設為“Edit”的Button, LinkButton, 或 ImageButton 被點時激發(fā).
CancelCommand event — 當CommandName屬性設為“Cancel”的Button, LinkButton, 或ImageButton 被點時激發(fā).
UpdateCommand event — 當CommandName屬性設為“Update”的Button,LinkButton, 或ImageButton 被點時激發(fā).
DeleteCommand event — 當CommandName屬性設為“Delete”的Button, LinkButton, 或 ImageButton 被點時激發(fā).
使用以上的屬性和事件,有四種方法來更新和刪除數(shù)據(jù):(第一種方法提供了更好的可擴展性,而設計DataList的本意就是使用這種方式。)
1. 使用ASP.NET 1.x 的技術— DataList先于ASP.NET 2.0 和ObjectDataSources 存在,可以直接通過編程來實現(xiàn)編輯和刪除。這種方法需要在顯示數(shù)據(jù)或者更新刪除記錄時,直接在BLL層將數(shù)據(jù)綁定到DataList。
2. 使用一個單獨的ObjectDataSource 來實現(xiàn)選擇,更新和刪除 — DataList沒有GridView內置的編輯刪除功能,并不意味著不能添加這些功能。使用 ObjectDataSource,但是在設置ObjectDataSource的參數(shù)并調用Update()方法時,需要為DataList的UpdateCommand事件創(chuàng)建一個 event handler。
3. Using an ObjectDataSource Control for Selecting, but Updating and Deleting Directly Against the BLL — 使用第二種方法時需要為UpdateCommand事件和參數(shù)賦值等寫一些代碼。其實我們可以用ObjectDataSource來實現(xiàn)selecting ,更新和刪除直接調用BLL(象第一種方法)。直接調用BLL會使代碼可讀性更好。
4. 使用多個ObjectDataSources —前面的三種方法都需要一些代碼。最后一種方法是使用多個ObjectDataSources 。第一個ObjectDataSource 從BLL獲取數(shù)據(jù),并綁定到 DataList. 為更新添加另一個 ObjectDataSource, 直接添加到DataList的 EditItemTemplate.同樣對刪除也是如此。三個ObjectDataSource通過ControlParameters聲明語法直接將參數(shù)綁定到ObjectDataSource 的參數(shù) (而不是在 DataList的 UpdateCommand event handler編程處理). 這種方法也需要一些編碼 — 需要調用ObjectDataSource內置的 Update() 或 Delete() — 但是比起其它三種方法,代碼少的多。這種方法的劣勢是多個ObjectDataSources 使頁面看起來混亂。
注意: 1. 當使用ObjectDataSource修改數(shù)據(jù)時,在聲明標記里需要移除OldValuesParameterFormatString (或重新設為缺省值,{0})。在并發(fā)控制下可使用original_{0},表示原始數(shù)據(jù)。
2. DataList有一些屬性是編輯和刪除需要用到的,這些值都存在view state里。因此創(chuàng)建支持編輯和刪除功能的DataList時,DataList的view state需要開啟。在創(chuàng)建可編輯的GridView,DetailsViews和FormViews的時候,view state是禁用的。這是因為ASP.NET 2.0 控件包含了control state,它在postback時狀態(tài)是連續(xù)的。在GridView里禁用了view state僅僅只是忽略了無關緊要的狀態(tài)信息,但是維持了control state(它包含了編輯和刪除需要的狀態(tài))。而DataList是 ASP.NET 1.x時代創(chuàng)建的,并沒有使用control state,因此view state必須開啟。
3.默認DataList只有一個ItemTemplate。需要手動添加一個EditItemTemplate來支持編輯功能。
4.DataList并不支持雙向綁定,準備更新數(shù)據(jù)時,需要編程將Textbox的Text的值傳給ProductsBLL類的UpdateProduct方法。
當設置了CommandName的Repeater或DataList里的Button,LinkButton或ImageButton被點擊時,Repeater或DataList的ItemCommand事件被激發(fā)。對DataList來說,如果CommandName設為某個值,另外一個事件也會被激發(fā)(除了ItemCommand被激發(fā)以外,下面事件也會激發(fā)),如下:
“Cancel” — 激發(fā) CancelCommand event
“Edit” — 激發(fā) EditCommand event
“Update” — 激發(fā)UpdateCommand event
點擊DataList里的button會引起postback,但是并沒有進入product的編輯模式。為了完成這個,需要:
1. 設置DataList的 EditItemIndex property 為 被點擊了Edit button的 DataListItem的 index .
2. 重新綁定數(shù)據(jù)到 DataList. 當 DataList 重新展現(xiàn)時, 和DataList的EditItemIndex相關的DataListItem 會展現(xiàn)EditItemTemplate.
通過以下代碼完成:
C#
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
// Set the DataList's EditItemIndex property to the index of the DataListItem that was clicked
DataList1.EditItemIndex = e.Item.ItemIndex;// EditItemIndex表示獲取或設置 DataList 控件中要編輯的選定項的索引號。
// Rebind the data to the DataList
DataList1.DataBind();
}
第二個參數(shù)類型為DataListCommandEventArgs ,它是被點擊的Edit button的DataListItem的引用(e.Item).首先設置DataList的EditItemIndex為需要編輯的DataListItem的ItemIndex,然后重新綁定數(shù)據(jù)。使DataList以只讀模式展示item,需要:
1. 設置DataList的 EditItemIndex property 為一個不存在的DataListItem index -1是一個好的選擇。(由于DataListItem index從0開始)
2. 重新綁定數(shù)據(jù)到DataList。由于沒有DataListItem ItemIndex和DataList的EditItemIndex關聯(lián),整個DataList會展現(xiàn)為只讀模式。
可以通過以下代碼完成:
C#
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
// Set the DataList's EditItemIndex property to -1
DataList1.EditItemIndex = -1;
// Rebind the data to the DataList
DataList1.DataBind();
}
完成UpdateCommand event handler,需要:
1.編程獲取用戶輸入的product name和price,還有ProductID.
2.調用ProductsBLL類里的合適的UpdateProduct重載方法.
3.設置DataList的EditItemIndex property 為一個不存在的DataListItem index. -1 是一個好的選擇。
4.重新幫訂數(shù)據(jù)。
下面的代碼完成了上面的功能:
C#
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
// Read in the ProductID from the DataKeys collection
int productID = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
// Read in the product name and price values
TextBox productName = (TextBox)e.Item.FindControl("ProductName");
TextBox unitPrice = (TextBox)e.Item.FindControl("UnitPrice");//查找對應的控件
string productNameValue = null;
if (productName.Text.Trim().Length > 0)
productNameValue = productName.Text.Trim();
decimal? unitPriceValue = null;
if (unitPrice.Text.Trim().Length > 0)
unitPriceValue = Decimal.Parse(unitPrice.Text.Trim(), System.Globalization.NumberStyles.Currency);
// Call the ProductsBLL's UpdateProduct method...
ProductsBLL productsAPI = new ProductsBLL();
productsAPI.UpdateProduct(productNameValue, unitPriceValue, productID);//調用BLL中的重載方法
// Revert the DataList back to its pre-editing state
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
為DataList的DeleteCommand事件創(chuàng)建一個event handler實現(xiàn)刪除功能,見下面的代碼:
C#
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
// Read in the ProductID from the DataKeys collection
int productID = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
// Delete the data
ProductsBLL productsAPI = new ProductsBLL();
productsAPI.DeleteProduct(productID);//調用BLL中的方法
// Rebind the data to the DataList
DataList1.DataBind();
}