【转贴】ListView控件学习系列2-编辑ListView


作者:方明

原贴地址:http://www.cnblogs.com/nuaalfm/archive/2008/09/02/1281885.html< p>


目录:

ListView控件学习系列1-了解ListView控件

ListView控件学习系列2-编辑ListView

ListView控件学习系列3-ListView选择,排序,分页

ListView使用技巧

源码下载

一、编辑,取消,更新操作

首先拖一个ListView控件到页面,然后按如下代码进行调整,这里为了利用ListView控件中按钮的内置功能CommandName必须和我们这里的名字一样

前端代码
<asp:ListView ID="ListView1" runat="server" OnItemEditing="ListView1_ItemEditing"
            OnItemCanceling="ListView1_ItemCanceling" OnItemUpdating="ListView1_ItemUpdating">
            <ItemTemplate>
                <tr>
                    <td>
                        <%#Eval("ID") %>
                    </td>
                    <td>
                        <%#Eval("name") %>
                    </td>
                    <td>
                        <asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit" />
                    </td>
                </tr>
            </ItemTemplate>
            <EditItemTemplate>
                <tr>
                    <td>
                        <asp:Label ID="IDLable" runat="server" Text='<%#Eval("ID") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' />
                    </td>
                    <td>
                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                    </td>
                </tr>
            </EditItemTemplate>
            <LayoutTemplate>
                <table>
                    <tr runat="server" id="itemPlaceholder">
                    </tr>
                </table>
            </LayoutTemplate>
        </asp:ListView>

这里我们在App_Code文件夹中添加DataAccess类来模拟数据提供,代码如下:

模拟数据提供代码
public class DataAccess
{
    public List<Employee> List;
    public DataAccess()
    {
        List = new List<Employee>();
        Employee e1 = new Employee {ID=1, Name = "lfm1", Age = 30 };
        Employee e2 = new Employee {ID=2, Name = "lfm2", Age = 30 };
        Employee e3 = new Employee {ID=3, Name = "lfm3", Age = 30 };
        Employee e4 = new Employee {ID=4, Name = "lfm4", Age = 30 };
        Employee e5 = new Employee {ID=5, Name = "lfm5", Age = 30 };
        Employee e6 = new Employee {ID=6, Name = "lfm6", Age = 30 };
        List.Add(e1);
        List.Add(e2);
        List.Add(e3);
        List.Add(e4);
        List.Add(e5);
        List.Add(e6);

}
}
public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public int Sex { get; set; }
}

页面的后台代码如下:

页面后台代码
public partial class _Default : System.Web.UI.Page
{
    DataAccess da = new DataAccess();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }
    private void Bind()
    {
        ListView1.DataSource = da.List;
        ListView1.DataBind();
    }
    protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        ListView1.EditIndex = e.NewEditIndex;
        Bind();
    }
    protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        foreach (var item in da.List)
        {
            if (item.ID.ToString() == ((Label)ListView1.Items[e.ItemIndex].FindControl("IDLable")).Text)
            {
                item.Name = ((TextBox)ListView1.Items[e.ItemIndex].FindControl("NameTextBox")).Text;
            }
        }
        ListView1.EditIndex = -1;
        Bind();
    }
    protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        ListView1.EditIndex = -1;
        Bind();
    }
}

这里需要注意在Page_Load中绑定时必须判断是否是回发,如果是回发状态则不能重新绑定,因为如果重新绑定则相应的事件不会被触发

二、删除操作

前端代码

前端代码
<asp:ListView ID="ListView1" runat="server" OnItemDeleting="ListView1_ItemDeleting">
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Label ID="IDLable" runat="server" Text='<%#Eval("ID") %>' />
                    </td>
                    <td>
                        <%#Eval("name") %>
                    </td>
                    <td>
                        <asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit" />
                    </td>
                    <td>
                        <asp:Button ID="DeleteButton" runat="server" Text="Delete" CommandName="Delete" />
                    </td>
                </tr>
            </ItemTemplate>
            <LayoutTemplate>
                <table>
                    <tr runat="server" id="itemPlaceholder">
                    </tr>
                </table>
            </LayoutTemplate>
</asp:ListView>

因为这里是模拟数据提供,所以数据的状态我们暂且用Session保存,后台代码如下:

后台代码
public partial class _Default : System.Web.UI.Page
{
    DataAccess da;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["da"] == null)
        {
            da = new DataAccess();
            Session["da"] = da;
        }
        else
        {
            da = Session["da"] as DataAccess;
        }
        if (!IsPostBack)
        {
            Bind();
        }
    }
    protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        foreach (var item in da.List)
        {
            string currentID = ((Label)ListView1.Items[e.ItemIndex].FindControl("IDLable")).Text;
            if (item.ID.ToString() == currentID)
            {
                da.List.Remove(item);
                break;
            }
        }
        Bind();
    }
}

三、插入操作

ListView的Insert Mode通过其属性InsertItemPosition来控制,该属性的取值有三种:

None:非Insert状态
FirstItem:Insert状态,且编辑模板显示于ListView所有item的最前面
LastItem :Insert状态,且编辑模板显示于ListView所有item的最后面

前端代码:

前端代码
<asp:ListView ID="ListView1" runat="server" OnItemInserting="ListView1_ItemInserting">
            <LayoutTemplate>
                <table id="Table1" runat="server" border="0" style="">
                    <tr runat="server" id="itemPlaceholder" />
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <%#Eval("ID") %>
                    </td>
                    <td>
                        <asp:Label ID="idLabel" runat="server" Text='<%# Eval("name") %>' />
                    </td>
                    <td>
                        <asp:Label ID="xLabel" runat="server" Text='<%# Eval("age") %>' />
                    </td>
                </tr>
            </ItemTemplate>
            <InsertItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
                    </td>
                    <td>
                        <asp:TextBox ID="IDTextBox" runat="server" />
                    </td>
                    <td>
                        <asp:TextBox ID="NameTextBox" runat="server" />
                    </td>
                    <td>
                        <asp:TextBox ID="AgeTextBox" runat="server" />
                    </td>
                </tr>
            </InsertItemTemplate>
        </asp:ListView>
        <asp:Button ID="Button1" runat="server" Text="插入" OnClick="Button1_Click" />

后台代码

后台代码

【转贴】ListView控件学习系列2-编辑ListView相关推荐

  1. ListView控件学习系列2-编辑ListView(Edit,Update,Insert,Delete)

    目录: ListView控件学习系列1-了解ListView控件 ListView控件学习系列2-编辑ListView ListView控件学习系列3-ListView选择,排序,分页 ListVie ...

  2. android中ListView控件onItemClick事件中获取listView传递的数据

    http://blog.csdn.net/aben_2005/article/details/6592205 本文转载自:android中ListView控件&&onItemClick ...

  3. android中的 listview控件,聊聊Android中的ListView控件

    软硬件环境 Macbook Pro MGX 72 Android Studio 1.3.2 坚果手机 前言 ListView是Android系统中使用非常广泛的一种控件,几乎所有的App都会用到它.它 ...

  4. Ajax Toolkit 控件学习系列(5) ——CalendarExtender使用

    Toolkit中给我们提供了很漂亮的CalendarExtender,而且我们还可以通过CSS自定义其显示的样式.今天简单学习下这个控件的简单使用. 还是先看效果. 首先,提示输入textbox中的内 ...

  5. Ajax Toolkit 控件学习系列(13) ——FilteredTextBoxExtender 控制输入

    这个控件的作用是对TextBox所要输入的内容进行过滤控制.按照自己需要过滤,可以自定义,再或者使用定义好的方式. 看效果. 效果不是很突出,说明下,就是只能输入大写字母和数字.因为加了限制,但是具体 ...

  6. 【VBA树控件学习四】编辑与删除TreeView节点

    遇见春天 HI,我是默默等你来点赞的edon,大家最近忙吗? 今天,我们接着来讲一下TreeView树控件.之前,我们已经把新增节点的功能讲完了,接下来我们来讲一下编辑功能与删除功能. 这里我们只修改 ...

  7. ListView控件的基本使用(方式一:使用ArrayAdapter适配器实现)

    ListView绝对可以称得上是Android中最常用的控件之一,几乎所有的应用程序都会用到它.由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示的时候 ...

  8. ListView控件和Adapter

    文章目录 1.ListView组件的作用 2.Adapter的作用 2.1 android adapter的类图 3.adapter的使用 3.1 ArrayAdapter(数组适配器) 案例 3.2 ...

  9. VBA自学应用(16)——Listview控件基本操作

    ListView控件基本操作 一.找到ListView控件 二.Listview控件显示数据 三.使用复选框 四.在listview控件中排序 一.找到ListView控件 工具--附加控件--Mic ...

最新文章

  1. 安装asp.net mvc4后mvc3项目编译报错
  2. MessageBox不能应用皮肤的解决办法
  3. vue商城项目开发:axios发送请求及列表数据展示
  4. swift4.2 - 一个自定义view弹框
  5. 第二个mysql怎么装_Linux下安装两个MySQL的方法
  6. 如何使用github中的pull request功能?
  7. boost::asio
  8. carrot2聚类的不同聚类算法 选用方法
  9. java大马后门_一款免杀php大马的解密与去后门
  10. 解决OverflowError: int too large to convert to float的办法
  11. linux相关rpm包下载地址
  12. 谈一下MVVM 框架
  13. 模拟轮盘抽奖游戏:一等奖、二等奖、三等奖
  14. 轮子王-原创数据结构-V2.0--内存//二级指针//小样儿练习
  15. ckfinder php,php,ckfinder_ckfinder mac下报错 linux正常,php,ckfinder - phpStudy
  16. 抗光幕布为什么深受家庭用户青睐?
  17. 国风·召南·野有死麕
  18. CoinUp开启GameFi新世界—魔法元世界(MAC)
  19. 已解决 阿里云盘 应用网络状况不佳,扫码,用账号登录,用手机号登录都不可以
  20. 微型计算机可以配置,目前主流微型计算机的配置及选购的调查报告

热门文章

  1. HTTP请求常见错误大全
  2. SysV和BSD启动风格的比较
  3. 表单流程中获取当前执行人填写的审批意见
  4. D3D 光照和材料 小样例
  5. hihoCoder #1033 : 交错和 [ 数位dp ]
  6. Typescript 下 Mongoose 外键类型外键数组类型定义类型保护联合类型理解
  7. 医疗大数据技术与应用
  8. yii 提交表单报400错误,提示 “您提交的数据无法验证”,问题处理。
  9. [每天一个知识点]31-乱七八糟-如何判断预言有效
  10. Illegal output or inout port connection (port 'out').