开发者

How to delete a Item in a ASP.NET listview and persist the datasource?

I have a collection of objects I bind to a Listview like this:

   if (!IsPostBack)
        {
            List<Equipment> persons = new List<Equipment>
                             {new Equipment{ItemName = "Sworn", ItemCount = 7, ItemCost = 255},
                              new Equipment{ItemName = "Civ", ItemCount = 3, ItemCost = 80},
                              new Equipment{ItemName = "Civ", ItemCount = 5, ItemCost = 200}};

            lvMain.DataSource = persons;

            BindList();
        }

I want to Add/update/delete from this object collection and submit the final data object collection to the BL when the user saves... Rather than just delete/add/update everytime a row is changed.

So my question is ho开发者_如何学Pythonw do I maintain state for that datasource? I have tried this (delete example)

  protected void lvMain_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Delete":
                {
                    ListViewDataItem lvdi = (ListViewDataItem)e.Item;
                    lvMain.Items.Remove(lvdi);                
                    break;
                }

But it does nothing. I can't rebind it to the datasrouce because at this point the datasource is null.. I assume the listview keeps its own view state which contains the data?... I guess worse case I can always hold the Object Collection in a Session object.. ..

Am I doing something wrong or thinking the wrong way?


My suggestion would be to store persons in the Viewstate, then add/edit/remove from this.

Store in Viewstate

ViewState["Persons"] = persons;

Get back out of Viewstate

List<Equipment> persons = (List<Equipment>)ViewState["Persons"];

... Perform add/edit/remove on object then store back in the Viewstate

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜