背景:VisualStudio2005
实现:DataList之数据读取、编辑、删改等功能
界面:
HTML:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datalist.aspx.cs" Inherits="datalist" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" Style="position: static;" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Width="345px" OnItemCommand="DataList1_ItemCommand" OnEditCommand="DataList1_EditCommand" OnCancelCommand="DataList1_CancelCommand" OnUpdateCommand="DataList1_UpdateCommand" OnDeleteCommand="DataList1_DeleteCommand">
            <ItemTemplate>
                &nbsp;
                <asp:LinkButton ID="lBtnShowDetails" runat="server" Style="left: -15px;
                    position: static; top: 0px" Width="55px" CommandName="select" >Details</asp:LinkButton>
                <asp:LinkButton ID="lBtnEdit" runat="server" CommandName="edit" Width="31px">Edit</asp:LinkButton>
                <asp:LinkButton ID="lBtnDelete" runat="server" CommandName="delete" Width="42px" OnClientClick="return confirm( '你确定要删除吗?');">Delete </asp:LinkButton><%# DataBinder.Eval(Container.DataItem,"LastName") %><%# DataBinder.Eval(Container.DataItem,"FirstName") %>
            </ItemTemplate>
            <SelectedItemTemplate>
            Name:<%# DataBinder.Eval(Container.DataItem, "LastName")%>
                 <%# DataBinder.Eval(Container.DataItem,"FirstName") %>
                 <br />
            Birth:<%# DataBinder.Eval(Container.DataItem,"BirthDate","{0:D}") %>
                 <br />
            City:<%# DataBinder.Eval(Container.DataItem,"City") %>
                 <br />
            Address:<%# DataBinder.Eval(Container.DataItem,"Address") %>
                <br />
                <asp:Button ID="btnExit" runat="server" OnClick="btnExit_Click" Text="Exit" Width="44px" />
            </SelectedItemTemplate>
            <FooterStyle BackColor="#CCCCCC" Font-Bold="True" Font-Italic="False" Font-Overline="False"
                Font-Strikeout="False" Font-Underline="False" ForeColor="Black" HorizontalAlign="Center" Font-Size="10pt" />
            <SelectedItemStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <AlternatingItemStyle BackColor="Gainsboro" Font-Bold="False" Font-Italic="False" Font-Overline="False"
                Font-Strikeout="False" Font-Underline="False" />
            <ItemStyle BackColor="#EEEEEE" ForeColor="Black" />
            <HeaderTemplate>
                Employees Information
            </HeaderTemplate>
            <HeaderStyle BackColor="#000084" Font-Bold="True" Font-Italic="False" Font-Overline="False"
                Font-Strikeout="False" Font-Underline="False" ForeColor="White" HorizontalAlign="Center" />
            <EditItemTemplate>
                <%# DataBinder.Eval(Container.DataItem,"LastName") %>
                <%# DataBinder.Eval(Container.DataItem,"FirstName") %>
                <br />
                <%# DataBinder.Eval(Container.DataItem,"City") %>
                <asp:TextBox ID="txtCity" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"City") %>' Width="94px"></asp:TextBox><br />
                <asp:LinkButton ID="LinkButton2" runat="server" CommandName="update" Width="40px" OnClientClick="return confirm('你确定保存吗?');">Save</asp:LinkButton>
                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="cancel">Cancel</asp:LinkButton>
            </EditItemTemplate>
        </asp:DataList>
        &nbsp; &nbsp; &nbsp;<br />
        <table style="width: 345px">
            <tr>
                <td align="center" style="width: 40px; height: 23px">
        <asp:Label ID="Label1" runat="server" Text="Total: " Width="35px"></asp:Label>
                </td>
                <td align="center" style="width: 56px; height: 23px">
                    &nbsp;<asp:Label
            ID="lblTotal" runat="server" Text="Label" Width="7px" Font-Bold="True" ForeColor="#0000C0"></asp:Label></td>
                <td align="center" style="width: 53px; height: 23px">
                    <asp:Label
                ID="Label3" runat="server" Text="Current: " Width="43px"></asp:Label></td>
                <td align="left" style="width: 160px; height: 23px">
                    <asp:Label
                    ID="lblCurrent" runat="server" Text="Label" Width="42px" Font-Bold="True" ForeColor="#0000C0"></asp:Label></td>
            </tr>
            <tr>
                <td align="center" style="width: 40px; height: 21px">
                    <asp:Button
                        ID="btnFirst" runat="server" OnClick="btnFirst_Click"
                        Text="First" Width="38px" /></td>
                <td align="center" style="width: 56px; height: 21px">
                    <asp:Button ID="btnPrevious" runat="server" OnClick="btnPrevious_Click" Text="Previous" Width="55px" /></td>
                <td align="center" style="width: 53px; height: 21px">
                    <asp:Button ID="btnNext"
                                runat="server" OnClick="btnNext_Click" Text="Next" Width="55px" /></td>
                <td align="left" style="width: 160px; height: 21px">
                    <asp:Button
                                    ID="btnLast" runat="server" OnClick="btnLast_Click"
                                    Text="Last" Width="38px" /></td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>
CODE:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class datalist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //初始化时设置当前页为第一页
            lblCurrent.Text = "1";
            bindDataToDatalist();
        }
    }
    private void bindDataToDatalist()
    {
        int currentPage = Convert.ToInt32(lblCurrent.Text);
        SqlConnection con = DB.createCon();
        //初始化SqlDataAdapter 实例
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = new SqlCommand("select * from employees", con);
        DataSet ds = new DataSet();
        sda.Fill(ds, "emp");
        //DataList控件在绑定数据时,应先将DataKeyField属性设置为数据表的主键。
        this.DataList1.DataKeyField = "EmployeeID";
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = ds.Tables["emp"].DefaultView;
        //启用分页
        ps.AllowPaging = true;
        //每页显示的项数
        ps.PageSize = 2;
        //获取当前页的索引,从0开始
        ps.CurrentPageIndex = currentPage - 1;
        this.btnFirst.Enabled = true;
        this.btnPrevious.Enabled = true;
        this.btnNext.Enabled = true;
        this.btnLast.Enabled = true;
        if (currentPage == 1)
        {
            btnFirst.Enabled = false;
            btnPrevious.Enabled = false;
        }
        //所有项所需的总页数
        if (currentPage == ps.PageCount )
        {
            btnLast.Enabled = false;
            btnNext.Enabled = false;
        }
        //this.DataList1.DataSource = ds.Tables["emp"];
        //将lblTotal设置为总页数
        this.lblTotal.Text = Convert.ToString(Convert.ToInt32(ps.PageCount));
        this.DataList1.DataSource = ps;
        this.DataList1.DataBind();
}
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "select")
        {
           //获取DataList控件中的选定项的索引;获取DataList.Item控件中的DataListItem对象的索引
            this.DataList1.SelectedIndex = e.Item.ItemIndex;
            this.bindDataToDatalist();
        }
    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        //设置选中行的索引为当前选择行的索引
        this.DataList1.EditItemIndex = e.Item.ItemIndex;
        this.bindDataToDatalist();
    }
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        //设置控件的编辑项的索引为-1,即取消编辑
        this.DataList1.EditItemIndex = -1;
        //取消选择项
        this.DataList1.SelectedIndex = -1;
        this.bindDataToDatalist();
    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        //查找索引对应的主键值,由DataKeys集合利用索引值取得各数据的索引值
        string empID = this.DataList1.DataKeys[e.Item.ItemIndex].ToString();
        //在编辑的行中查找对应的要更改的城市的名字,用FindControl()查找txtCity控件
        string city = ((TextBox)(e.Item.FindControl("txtCity"))).Text;
        SqlConnection con = DB.createCon();
        SqlCommand cmd = new SqlCommand("update employees_new set City='" + city + "' where EmployeeID='" + empID + "'", con);
        con.Open();
        cmd.ExecuteNonQuery();
        //设置选中行的索引为-1,取消该数据项的选择
        this.DataList1.EditItemIndex = -1;
        this.bindDataToDatalist();
}
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        string empID = this.DataList1.DataKeys[e.Item.ItemIndex].ToString();
        SqlConnection con = DB.createCon();
        SqlCommand cmd = new SqlCommand("delete from employees_new where EmployeeID='" + empID + "'", con);
        con.Open();
        //对连接执行T-SQL语句,并返回受影响的行数
        cmd.ExecuteNonQuery();
        this.DataList1.EditItemIndex = -1;
        this.bindDataToDatalist();
    }
protected void btnFirst_Click(object sender, EventArgs e)
    {
        this.lblCurrent.Text = "1";
        this.bindDataToDatalist();
    }
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        this.lblCurrent.Text = Convert.ToString(Convert.ToInt32(this.lblCurrent.Text) - 1);
        this.bindDataToDatalist();
            }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        this.lblCurrent.Text = Convert.ToString(Convert.ToInt32(this.lblCurrent.Text) + 1);
        this.bindDataToDatalist();
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        this.lblCurrent.Text = this.lblTotal.Text;
        this.bindDataToDatalist();
    }
//退出查看详细信息模式
    protected void btnExit_Click(object sender, EventArgs e)
    {
        this.DataList1.SelectedIndex = -1;
        this.bindDataToDatalist();
    }

  
}
运行效果:
详细信息:
编辑:
更新:
删除:

转载于:https://blog.51cto.com/hndtraveller/179464

DataList之数据操作相关推荐

  1. numpy和torch数据操作对比

    对numpy和torch数据操作进行对比,避免遗忘. ndarray和tensor import torch import numpy as npnp_data = np.arange(6).resh ...

  2. Redis数据库搭建主从同步(主从概念、主从配置、主从数据操作)

    1. 主从概念 ⼀个master可以拥有多个slave,⼀个slave⼜可以拥有多个slave,如此下去,形成了强⼤的多级服务器集群架构 master用来写数据,slave用来读数据,经统计:网站的读 ...

  3. 命令行客户端MySQL基本命令的使用(登录、登出、数据库操作的SQL语句、表结构的SQL语句、表数据操作的SQL语句)

    1. 登录和登出数据库 登录数据库: 输入下面命令: mysql -uroot -p 说明: -u 后面是登录的用户名  [写成-u root也是可以的] -p 后面是登录密码, 如果不填写, 回车之 ...

  4. MySQL基础篇:数据操作语言DML

    1.概述 数据操作语言(DML)用于插入.修改.删除.查询数据记录,包括以下SQL语句: INSERT:添加数据到数据库中 UPDATE:修改数据库中的数据 DELETE:删除数据库中的数据 2.插入 ...

  5. 逻辑模型三要素-数据操作

    数据操作是指对数据库中各种对象的实例或取值所允许执行操作的集合,其中包括操作方法及有关规则,它是对数据库动态特性的描述.

  6. 使用dplyr进行数据操作(30个实例)

    本文转载自"R语言",已获授权. dplyr软件包是R中功能最强大,最受欢迎的软件包之一.该软件包由最受欢迎的R程序员Hadley Wickham编写,他编写了许多有用的R软件包, ...

  7. Spring中集成ActiveRecordPlugin数据操作插件

    2019独角兽企业重金招聘Python工程师标准>>> ###Spring集成ActiveRecordPlugin数据操作插件 package com.yunhetong.mq.se ...

  8. MySQL:数据操作

    数据操作 一.输入数据 格式: mysql>INSERT INTO 数据表名 VALUES(输入数据); 例 mysql> INSERT INTO shujuleixing_xiaoshu ...

  9. 表操作,数据操作,单表查询,python操作数据库

    1.表操作 创建 create table 删除 drop table 查看表结构 desc 表/show create table 表 修改 alter table 表名 rename 新表名 al ...

最新文章

  1. 领课网络在线教育系统开源项目
  2. golang之包和锁的机制
  3. python urllib.request 爬虫 数据处理-python 爬虫之 urllib库
  4. 在联盛德W600 SDK上添加C文件并实现GCC编译
  5. [转]Myeclipse9引入easy_ui时,其中的部分js文件报错
  6. 开源:秋式广告杀手源码
  7. AdapterViewlt;?gt; arg0, View arg1, int arg2, long arg3參数含义
  8. html三列布局和两列布局,CSS 常见两列布局、三列布局
  9. 一、1.1 Kaggle中kernel技巧
  10. CSS最基础的语法和三种引入方式
  11. 快速自检电脑是否被黑客入侵过(Windows版)
  12. 自建gitHub的webhook消息通知设计思路及成功验证结果
  13. 火狐浏览器50Linux32位,火狐浏览器32位完整离线安装包下载
  14. 定级阿里P7,300道Java面试题帮你全副武装
  15. 属性编辑器PropertyEditor
  16. SylixOS 技术简介
  17. 如何设计大自然?#合成美学 #无学科专栏
  18. ENVI制作土壤干旱墒情专题图/地域干旱分布
  19. 水泥cement一词在英语里有时写作caement
  20. python连乘函数_(Python3) 连加 连乘 代码

热门文章

  1. GIMP永久保存选择的办法
  2. 首次使用eclipes运行项目报错“Java.lang.ClassNotFoundException”
  3. 第四次工业革命:人工智能(AI)入门
  4. hibernate5.2.10.Final基本配置
  5. Activity的taskAffinity属性
  6. 网站移动版本开发踩坑实录二
  7. Flex 当鼠标悬停在DataGrid某行上时用datatoolField显示当前行
  8. 第二阶段冲刺第五天站立会议
  9. 【Javascript】复选框的全选与全不选
  10. 算法笔记_028:字符串转换成整数(Java)