如何删除一个项目在ASP.NET ListView和持续数据源? [英] How to delete a Item in a ASP.NET listview and persist the datasource?

查看:366
本文介绍了如何删除一个项目在ASP.NET ListView和持续数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的集合我绑定到一个像这样的列表视图:

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

我要添加/更新/从这个对象集合中删除,并提交最终的数据对象集合到BL当用户节省了......而不是仅仅删除/添加/更新每次一排被改变。

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

但不起任何作用。我不能因为这点数据源为null,重新绑定到datasrouce ..我想在ListView保留其中包含的数据作为其自己的观点状态呢?......我想最坏的情况下我可以一直拥有对象集合在一个会话对象.. ..

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?

推荐答案

我的建议是存储在ViewState中的人,然后添加/编辑/从这个删除。

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

存储在ViewState中

Store in Viewstate

ViewState["Persons"] = persons;

找回了视图状态

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

...进行添加/编辑/删除的对象,然后回到存储在ViewState

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

这篇关于如何删除一个项目在ASP.NET ListView和持续数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆