2017-11-20 4 views
1

웹 양식 페이지를 만들고 웹 검색 및 스택 오버플로 커뮤니티의 도움으로 많은 성과를 거두었지만 경험이 부족한 다른 문제가 발생했습니다. 여기 <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"> 

여기 코드는 SelectMethod, InsertMethod, UpdateMethodDeleteMethod위한 뒤에 있습니다 :

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)); 
     } 
    } 

첫 번째 세 가지 방법 모두 정확히 무엇을합니까 배고 있다. 예를 들어 내 페이지에서 업데이트 버튼을 클릭하면 lvLeadershipPositions_UpdateItem 메서드가 호출되고 lvLeadershipPositions_GetData 메서드가 호출됩니다.

그리고 삭제 버튼 lvLeadershipPositions_DeleteItem이 호출되었지만 lvLeadershipPositions_GetData은 호출되지 않으므로 페이지가 삭제되지 않으므로 업데이트되지 않습니다.

무엇을 버렸습니까?

+0

TryUpdateModel (항목)가 누락되었을 수 있습니다. in delete method – Giox

답변

1

데이터 소스에서 항목을 제거했지만 목록보기를 리 바인드하지 않았다고 생각합니다.

lvLeadershipPositions.DataBind()으로 리 바인드하거나 데이터 바인딩 호출이있는 TryUpdateModel (item) 메서드를 호출하십시오.

+0

포인터를 가져 주셔서 감사합니다. 내일 아침에 시도해 보겠습니다. 'lvLeadershipPositions.DataSource'와'.DataBind'를 사용하여 데이터 리 바인딩을 시도했지만 TryUpdateModel을 추가하지 않았기 때문에 아침에 시도하고 이에 따라이 게시물을 업데이트 할 것입니다. – cptully

+0

포인터 주셔서 감사! 나는'.DataSource()'줄을 빠져 나가서'.DataBind()'줄을 추가하고'TryUpdateModel (Item)'호출을 생략해야했습니다. 그러나 나는 그것을 작동하게했다! !!! – cptully