<asp:listview deletemethod =“”>没有更新UI

时间:2017-11-20 22:35:11

标签: c# asp.net listview

我正在构建一个webforms页面,并通过Stack Overflow社区的Web搜索和帮助取得了很多进展,但是我遇到了另一个障碍,可能是由于我自己的经验不足。

这是我的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));
        }
    }

前三种方法都完全符合我的预期。例如,单击页面上的更新按钮时,将调用lvLeadershipPositions_UpdateItem方法,然后调用lvLeadershipPositions_GetData方法。

当我点击删除按钮时lvLeadershipPositions_DeleteItem被调用,但lvLeadershipPositions_GetData从未被调用,因此页面不会更新以反映我的删除。

我遗漏了什么?

1 个答案:

答案 0 :(得分:1)

我认为您已从数据源中删除了该项,但您无法反弹列表视图。

使用 lvLeadershipPositions.DataBind()强制重新绑定,或者调用您的方法TryUpdateModel(item),其中可能存在DataBind调用。