国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
在DataList里編輯和刪除數(shù)據(jù)
  使用GridView來編輯和刪除數(shù)據(jù)之所以很簡單,是因?yàn)镚ridView和ObjectDataSource在底層非常一致。當(dāng)更新按鈕被點(diǎn)擊時(shí),GridView自動將字段的值賦給ObjectDataSource的UpdateParameters集合,然后激發(fā)ObjectDataSource的Update()方法。而DataList與Repeater并沒有相關(guān)使用方法。

    需要確保將合適的值賦給ObjectDataSource的參數(shù),然后調(diào)用Update()方法。DataList提供了以下的屬性和事件來完成這些:
    DataKeyField property — 更新或刪除時(shí),需要唯一確定DataList里的每個(gè)item。將這個(gè)屬性設(shè)為顯示的數(shù)據(jù)的主健。這樣做會產(chǎn)生DataList的 DataKeys collection ,每個(gè)item都有一個(gè)指定的 DataKeyField .
    EditCommand event — 當(dāng)CommandName屬性設(shè)為“Edit”的Button, LinkButton, 或 ImageButton 被點(diǎn)時(shí)激發(fā).
    CancelCommand event — 當(dāng)CommandName屬性設(shè)為“Cancel”的Button, LinkButton, 或ImageButton 被點(diǎn)時(shí)激發(fā).
    UpdateCommand event — 當(dāng)CommandName屬性設(shè)為“Update”的Button,LinkButton, 或ImageButton 被點(diǎn)時(shí)激發(fā).
    DeleteCommand event — 當(dāng)CommandName屬性設(shè)為“Delete”的Button, LinkButton, 或 ImageButton 被點(diǎn)時(shí)激發(fā).
    使用以上的屬性和事件,有四種方法來更新和刪除數(shù)據(jù):(第一種方法提供了更好的可擴(kuò)展性,而設(shè)計(jì)DataList的本意就是使用這種方式。)
    1. 使用ASP.NET 1.x 的技術(shù)— DataList先于ASP.NET 2.0 和ObjectDataSources 存在,可以直接通過編程來實(shí)現(xiàn)編輯和刪除。這種方法需要在顯示數(shù)據(jù)或者更新刪除記錄時(shí),直接在BLL層將數(shù)據(jù)綁定到DataList。
    2. 使用一個(gè)單獨(dú)的ObjectDataSource 來實(shí)現(xiàn)選擇,更新和刪除 — DataList沒有GridView內(nèi)置的編輯刪除功能,并不意味著不能添加這些功能。使用 ObjectDataSource,但是在設(shè)置ObjectDataSource的參數(shù)并調(diào)用Update()方法時(shí),需要為DataList的UpdateCommand事件創(chuàng)建一個(gè) event handler。
    3. Using an ObjectDataSource Control for Selecting, but Updating and Deleting Directly Against the BLL — 使用第二種方法時(shí)需要為UpdateCommand事件和參數(shù)賦值等寫一些代碼。其實(shí)我們可以用ObjectDataSource來實(shí)現(xiàn)selecting ,更新和刪除直接調(diào)用BLL(象第一種方法)。直接調(diào)用BLL會使代碼可讀性更好。
    4. 使用多個(gè)ObjectDataSources —前面的三種方法都需要一些代碼。最后一種方法是使用多個(gè)ObjectDataSources 。第一個(gè)ObjectDataSource 從BLL獲取數(shù)據(jù),并綁定到 DataList. 為更新添加另一個(gè) ObjectDataSource, 直接添加到DataList的 EditItemTemplate.同樣對刪除也是如此。三個(gè)ObjectDataSource通過ControlParameters聲明語法直接將參數(shù)綁定到ObjectDataSource 的參數(shù) (而不是在 DataList的 UpdateCommand event handler編程處理). 這種方法也需要一些編碼 — 需要調(diào)用ObjectDataSource內(nèi)置的 Update() 或 Delete() — 但是比起其它三種方法,代碼少的多。這種方法的劣勢是多個(gè)ObjectDataSources 使頁面看起來混亂。
    注意: 1. 當(dāng)使用ObjectDataSource修改數(shù)據(jù)時(shí),在聲明標(biāo)記里需要移除OldValuesParameterFormatString (或重新設(shè)為缺省值,{0})。在并發(fā)控制下可使用original_{0},表示原始數(shù)據(jù)。
                 2. DataList有一些屬性是編輯和刪除需要用到的,這些值都存在view state里。因此創(chuàng)建支持編輯和刪除功能的DataList時(shí),DataList的view state需要開啟。在創(chuàng)建可編輯的GridView,DetailsViews和FormViews的時(shí)候,view state是禁用的。這是因?yàn)锳SP.NET 2.0 控件包含了control state,它在postback時(shí)狀態(tài)是連續(xù)的。在GridView里禁用了view state僅僅只是忽略了無關(guān)緊要的狀態(tài)信息,但是維持了control state(它包含了編輯和刪除需要的狀態(tài))。而DataList是 ASP.NET 1.x時(shí)代創(chuàng)建的,并沒有使用control state,因此view state必須開啟。
               3.默認(rèn)DataList只有一個(gè)ItemTemplate。需要手動添加一個(gè)EditItemTemplate來支持編輯功能。
               4.DataList并不支持雙向綁定,準(zhǔn)備更新數(shù)據(jù)時(shí),需要編程將Textbox的Text的值傳給ProductsBLL類的UpdateProduct方法。
    當(dāng)設(shè)置了CommandName的Repeater或DataList里的Button,LinkButton或ImageButton被點(diǎn)擊時(shí),Repeater或DataList的ItemCommand事件被激發(fā)。對DataList來說,如果CommandName設(shè)為某個(gè)值,另外一個(gè)事件也會被激發(fā)(除了ItemCommand被激發(fā)以外,下面事件也會激發(fā)),如下:
“Cancel” — 激發(fā) CancelCommand event
“Edit” — 激發(fā) EditCommand event
“Update” — 激發(fā)UpdateCommand event
    點(diǎn)擊DataList里的button會引起postback,但是并沒有進(jìn)入product的編輯模式。為了完成這個(gè),需要:
1. 設(shè)置DataList的 EditItemIndex property 為 被點(diǎn)擊了Edit button的 DataListItem的 index .
2. 重新綁定數(shù)據(jù)到 DataList. 當(dāng) DataList 重新展現(xiàn)時(shí), 和DataList的EditItemIndex相關(guān)的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表示獲取或設(shè)置 DataList 控件中要編輯的選定項(xiàng)的索引號。
     // Rebind the data to the DataList
   DataList1.DataBind();
}
    第二個(gè)參數(shù)類型為DataListCommandEventArgs ,它是被點(diǎn)擊的Edit button的DataListItem的引用(e.Item).首先設(shè)置DataList的EditItemIndex為需要編輯的DataListItem的ItemIndex,然后重新綁定數(shù)據(jù)。使DataList以只讀模式展示item,需要:
1. 設(shè)置DataList的 EditItemIndex property 為一個(gè)不存在的DataListItem index -1是一個(gè)好的選擇。(由于DataListItem index從0開始)
2. 重新綁定數(shù)據(jù)到DataList。由于沒有DataListItem ItemIndex和DataList的EditItemIndex關(guān)聯(lián),整個(gè)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.調(diào)用ProductsBLL類里的合適的UpdateProduct重載方法.
3.設(shè)置DataList的EditItemIndex property 為一個(gè)不存在的DataListItem index. -1 是一個(gè)好的選擇。
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");//查找對應(yīng)的控件
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);//調(diào)用BLL中的重載方法
            // Revert the DataList back to its pre-editing state
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
    為DataList的DeleteCommand事件創(chuàng)建一個(gè)event handler實(shí)現(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);//調(diào)用BLL中的方法
      // Rebind the data to the DataList
DataList1.DataBind();
}
 
本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/lanwilliam/archive/2008/06/08/2522553.aspx
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
ASP.NET 2.0 數(shù)據(jù)操作:插入、更新、刪除數(shù)據(jù)時(shí)的事件
GridView里的Button控件用法
DataList內(nèi)容詳解
ASP.NET 2.0中實(shí)現(xiàn)模板中的數(shù)據(jù)綁定
DataList與Repeater
DataList嵌套式導(dǎo)航
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服