2017-11-20 7 views
1

私はWebフォームのページを作成し、Web検索で多くの進歩を遂げ、Stack Overflowコミュニティの助けを借りていますが、ここで <asp:ListView DeleteMethod> UIを更新していません

は私のListViewの開始タグである:

   <asp:ListView ID="lvLeadershipPositions" 
       DataKeyNames="ID" 
       InsertItemPosition="LastItem" 
       runat="server" 
       ItemType="BusinessLogic.Alumni.Outreach.ServicePosition" 
       SelectMethod="lvLeadershipPositions_GetData" 
       InsertMethod="lvLeadershipPositions_InsertItem" 
       UpdateMethod="lvLeadershipPositions_UpdateItem" 
       DeleteMethod="lvLeadershipPositions_DeleteItem"> 

、ここでコードがSelectMethodInsertMethodUpdateMethodDeleteMethodのための背後にある:

public IEnumerable<ServicePosition> lvLeadershipPositions_GetData() 
    { 
     if (alumn == null) 
     { 
      alumn = new Alumn(); 
     } 

     return alumn.LeadershipRoles; 
    } 

    public void lvLeadershipPositions_InsertItem() 
    { 
     var item = new BusinessLogic.Alumni.Outreach.ServicePosition(); 
     TryUpdateModel(item); 
     if (ModelState.IsValid) 
     { 
      // Save changes here 
      item.ID = alumn.LeadershipRoles.Count; 
      alumn.LeadershipRoles.Add(item); 
     } 
    } 

    // The id parameter name should match the DataKeyNames value set on the control 
    public void lvLeadershipPositions_UpdateItem(int id) 
    { 
     ServicePosition item = alumn.LeadershipRoles.Find(x => x.ID == id); 
     // Load the item here, e.g. item = MyDataLayer.Find(id); 
     if (item == null) 
     { 
      // The item wasn't found 
      ModelState.AddModelError("", String.Format("Item with id {0} was not found", id)); 
      return; 
     } 
     TryUpdateModel(item); 
     if (ModelState.IsValid) 
     { 
      // Save changes here, e.g. MyDataLayer.SaveChanges(); 

     } 
    } 

    // The id parameter name should match the DataKeyNames value set on the control 
    public void lvLeadershipPositions_DeleteItem(int id) 
    { 
     int count = alumn.LeadershipRoles.Count(x => x.ID == id); 
     if (count == 1) 
     { 
      int removeID = alumn.LeadershipRoles.Where(x => x.ID == id).First().ID; 
      alumn.LeadershipRoles.RemoveAt(removeID); 
      return; 
     } 
     else if (count == 0) 
     { 
      ModelState.AddModelError("", String.Format("Item with id {0} was not found", id)); 
      return; 
     } 
     else 
     { 
      ModelState.AddModelError("", String.Format("More than one Item with id {0} was found", id)); 
     } 
    } 

最初の三つの方法のすべてを正確に何をすI期待する。たとえば、私のページの更新ボタンをクリックすると、lvLeadershipPositions_UpdateItemメソッドが呼び出され、lvLeadershipPositions_GetDataメソッドが呼び出されます。

削除ボタンをクリックするとlvLeadershipPositions_DeleteItemが呼び出されますが、lvLeadershipPositions_GetDataは呼び出されません。そのため、ページは削除されません。

私は何を残しましたか?

+0

TryUpdateModel(項目)が不足している可能性があります。削除方法 – Giox

答えて

1

データソースからアイテムを削除しましたが、リストビューをリバウンドしていないと思います。

lvLeadershipPositions.DataBind()を使用して強制的にリバインドするか、TryUpdateModel(item)メソッドを呼び出します。ここには、おそらくDataBind呼び出しがあります。

+0

ポインタをありがとう、私は明日の朝にそれを試してみます。 'lvLeadershipPositions.DataSource'と' .DataBind'でデータを再バインドしようとしましたが、TryUpdateModelを追加しようとしませんでしたので、午前に試してみましょう。 – cptully

+0

ポインタありがとう!私は '.DataSource()'行を省略し、 '.DataBind()'行を追加し、 'TryUpdateModel(Item)'呼び出しを省略しなければなりませんでした。しかし、私はそれが働いている! – cptully

関連する問題