乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      在DataList里編輯和刪除數(shù)據(jù)

       狼志凌云 2010-12-18
        使用GridView來編輯和刪除數(shù)據(jù)之所以很簡單,是因為GridView和ObjectDataSource在底層非常一致。當(dāng)更新按鈕被點擊時,GridView自動將字段的值賦給ObjectDataSource的UpdateParameters集合,然后激發(fā)ObjectDataSource的Update()方法。而DataList與Repeater并沒有相關(guān)使用方法。

          需要確保將合適的值賦給ObjectDataSource的參數(shù),然后調(diào)用Update()方法。DataList提供了以下的屬性和事件來完成這些:
          DataKeyField property — 更新或刪除時,需要唯一確定DataList里的每個item。將這個屬性設(shè)為顯示的數(shù)據(jù)的主健。這樣做會產(chǎn)生DataList的 DataKeys collection ,每個item都有一個指定的 DataKeyField .
          EditCommand event — 當(dāng)CommandName屬性設(shè)為“Edit”的Button, LinkButton, 或 ImageButton 被點時激發(fā).
          CancelCommand event — 當(dāng)CommandName屬性設(shè)為“Cancel”的Button, LinkButton, 或ImageButton 被點時激發(fā).
          UpdateCommand event — 當(dāng)CommandName屬性設(shè)為“Update”的Button,LinkButton, 或ImageButton 被點時激發(fā).
          DeleteCommand event — 當(dāng)CommandName屬性設(shè)為“Delete”的Button, LinkButton, 或 ImageButton 被點時激發(fā).
          使用以上的屬性和事件,有四種方法來更新和刪除數(shù)據(jù):(第一種方法提供了更好的可擴(kuò)展性,而設(shè)計DataList的本意就是使用這種方式。)
          1. 使用ASP.NET 1.x 的技術(shù)— DataList先于ASP.NET 2.0 和ObjectDataSources 存在,可以直接通過編程來實現(xiàn)編輯和刪除。這種方法需要在顯示數(shù)據(jù)或者更新刪除記錄時,直接在BLL層將數(shù)據(jù)綁定到DataList。
          2. 使用一個單獨的ObjectDataSource 來實現(xiàn)選擇,更新和刪除 — DataList沒有GridView內(nèi)置的編輯刪除功能,并不意味著不能添加這些功能。使用 ObjectDataSource,但是在設(shè)置ObjectDataSource的參數(shù)并調(diào)用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 ,更新和刪除直接調(diào)用BLL(象第一種方法)。直接調(diào)用BLL會使代碼可讀性更好。
          4. 使用多個ObjectDataSources —前面的三種方法都需要一些代碼。最后一種方法是使用多個ObjectDataSources 。第一個ObjectDataSource 從BLL獲取數(shù)據(jù),并綁定到 DataList. 為更新添加另一個 ObjectDataSource, 直接添加到DataList的 EditItemTemplate.同樣對刪除也是如此。三個ObjectDataSource通過ControlParameters聲明語法直接將參數(shù)綁定到ObjectDataSource 的參數(shù) (而不是在 DataList的 UpdateCommand event handler編程處理). 這種方法也需要一些編碼 — 需要調(diào)用ObjectDataSource內(nèi)置的 Update() 或 Delete() — 但是比起其它三種方法,代碼少的多。這種方法的劣勢是多個ObjectDataSources 使頁面看起來混亂。
          注意: 1. 當(dāng)使用ObjectDataSource修改數(shù)據(jù)時,在聲明標(biāo)記里需要移除OldValuesParameterFormatString (或重新設(shè)為缺省值,{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僅僅只是忽略了無關(guān)緊要的狀態(tài)信息,但是維持了control state(它包含了編輯和刪除需要的狀態(tài))。而DataList是 ASP.NET 1.x時代創(chuàng)建的,并沒有使用control state,因此view state必須開啟。
                     3.默認(rèn)DataList只有一個ItemTemplate。需要手動添加一個EditItemTemplate來支持編輯功能。
                     4.DataList并不支持雙向綁定,準(zhǔn)備更新數(shù)據(jù)時,需要編程將Textbox的Text的值傳給ProductsBLL類的UpdateProduct方法。
          當(dāng)設(shè)置了CommandName的Repeater或DataList里的Button,LinkButton或ImageButton被點擊時,Repeater或DataList的ItemCommand事件被激發(fā)。對DataList來說,如果CommandName設(shè)為某個值,另外一個事件也會被激發(fā)(除了ItemCommand被激發(fā)以外,下面事件也會激發(fā)),如下:
      “Cancel” — 激發(fā) CancelCommand event
      “Edit” — 激發(fā) EditCommand event
      “Update” — 激發(fā)UpdateCommand event
          點擊DataList里的button會引起postback,但是并沒有進(jìn)入product的編輯模式。為了完成這個,需要:
      1. 設(shè)置DataList的 EditItemIndex property 為 被點擊了Edit button的 DataListItem的 index .
      2. 重新綁定數(shù)據(jù)到 DataList. 當(dāng) DataList 重新展現(xiàn)時, 和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 控件中要編輯的選定項的索引號。
           // Rebind the data to the DataList
         DataList1.DataBind();
      }
          第二個參數(shù)類型為DataListCommandEventArgs ,它是被點擊的Edit button的DataListItem的引用(e.Item).首先設(shè)置DataList的EditItemIndex為需要編輯的DataListItem的ItemIndex,然后重新綁定數(shù)據(jù)。使DataList以只讀模式展示item,需要:
      1. 設(shè)置DataList的 EditItemIndex property 為一個不存在的DataListItem index -1是一個好的選擇。(由于DataListItem index從0開始)
      2. 重新綁定數(shù)據(jù)到DataList。由于沒有DataListItem ItemIndex和DataList的EditItemIndex關(guān)聯(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.調(diào)用ProductsBLL類里的合適的UpdateProduct重載方法.
      3.設(shè)置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");//查找對應(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)建一個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);//調(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ǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多