웹 양식 페이지를 만들고 웹 검색 및 스택 오버플로 커뮤니티의 도움으로 많은 성과를 거두었지만 경험이 부족한 다른 문제가 발생했습니다. 여기 <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
, UpdateMethod
및 DeleteMethod
위한 뒤에 있습니다 :
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
은 호출되지 않으므로 페이지가 삭제되지 않으므로 업데이트되지 않습니다.
무엇을 버렸습니까?
TryUpdateModel (항목)가 누락되었을 수 있습니다. in delete method – Giox