使用ListView控件进行修改,删除与添加操作
1.页面代码:

<asp:ListView ID="lv2" runat="server"
onpagepropertieschanging="lv_PagePropertiesChanging2"
onitemcanceling="lv2_ItemCanceling" onitemediting="lv2_ItemEditing"
onitemupdating="lv2_ItemUpdating"
onitemdeleting="lv2_ItemDeleting" DataKeyNames="id">
<EditItemTemplate>
<div class="lvData">
<div class="id"><%#eval_r("id")%></div>
<div class="proId"><%#eval_r("provinceID")%></div>
<div class="proName">
<asp:TextBox ID="txt" runat="server" Text='<%#eval_r("province")%>'></asp:TextBox></div>
<div class="edit">
<asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Update" >更新</asp:LinkButton>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Cancel" >取消</asp:LinkButton>
</div>
</div>
</EditItemTemplate>
<ItemTemplate>
<div class="lvData">
<div class="id"><%#eval_r("id")%></div>
<div class="proId"><%#eval_r("provinceID")%></div>
<div class="proName"><%#eval_r("province")%></div>
<div class="edit">
<asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit" >编辑</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" >删除</asp:LinkButton>
</div>
</div>
</ItemTemplate>
<AlternatingItemTemplate>
<div class="lvData alterStyle">
<div class="id"><%#eval_r("id")%></div>
<div class="proId"><%#eval_r("provinceID")%></div>
<div class="proName"><%#eval_r("province")%></div>
<div class="edit">
<asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit" >编辑</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="Delete" >删除</asp:LinkButton>
</div>
</div>
</AlternatingItemTemplate>
</asp:ListView>
<asp:DataPager ID="DataPager2" PagedControlID="lv2" runat="server">
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton="True"
ShowLastPageButton="True" ShowNextPageButton="True"
ShowPreviousPageButton="True" />
</Fields>
</asp:DataPager>

2.后置代码:

public partial class index : System.Web.UI.Page
{
DBHelper db = new DBHelper();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
Lv2Bind();//更新值
}
} protected void lv_PagePropertiesChanging2(object sender, PagePropertiesChangingEventArgs e)
{
this.DataPager2.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
Lv2Bind();
} protected void lv2_ItemEditing(object sender, ListViewEditEventArgs e)
{
lv2.EditIndex = e.NewEditIndex;
Lv2Bind();
} protected void lv2_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
int index = e.ItemIndex;
TextBox txtbox = lv2.Items[index].FindControl("txt") as TextBox;
string proName = txtbox.Text;
string id=lv2.DataKeys[index].Value.ToString();
string sql = string.Format("UPDATE [Public].[dbo].[Province] SET [province] =@province WHERE id=@id");
SqlParameter[] paras = {
new SqlParameter("@province",proName),
new SqlParameter("@id",id)
};
if (db.ExecuteNonQuery(sql, paras) > )
{
this.divAlert.Attributes.CssStyle.Add("display", "block");
Lv2Bind();
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>更新失败</script>");
Lv2Bind();
}
} protected void lv2_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
lv2.EditIndex = -;
Lv2Bind();
} protected void lv2_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
} private void Lv2Bind()
{
this.lv2.DataSource = GetDT();
this.lv2.DataBind();
}
}

注意要点:
1.在Page_Load事件中,要把控件绑定数据的方法放在ispostback方法里面,以避免在页面加载的时候首
都要加载原来的数据,修改的数据无法更新的情况。
2.使用DataPager控件给ListView控件分页时,需要编写ListView控件的lv_PagePropertiesChanging()
时间,以使在进行翻页操作时控件的数据能及时更新到相应页面。

ASP.NET使用ListView数据绑定控件和DataPager实现数据分页显示(二)的更多相关文章

  1. ASP.NET使用ListView数据绑定控件和DataPager实现数据分页显示(一)

    为什么使用ListView+DataPager的方式实现分页显示? .net提供的诸多数据绑定控件,每一种都有它自己的优点和缺点.如果需要对数据进行操作,如果数据量不大的情况下,DataList和Gr ...

  2. asp.net学习之 数据绑定控件--表格绑定控件

    原文:asp.net学习之 数据绑定控件--表格绑定控件     数据绑定 Web 服务器控件是指可绑定到数据源控件,以实现在 Web 应用程序中轻松显示和修改数据的控件.数据绑定 Web 服务器控件 ...

  3. asp.net学习之 数据绑定控件--List数据绑定控件

    原文:asp.net学习之 数据绑定控件--List数据绑定控件 List控件(如 CheckBoxList.DropDownList.ListBox 和 RadioButtonList 类)继承自L ...

  4. asp.net学习之数据绑定控件、数据源控件概述

    原文:asp.net学习之数据绑定控件.数据源控件概述 1.asp.net数据绑定控件分为三大类,每个类分别进行详细:      ● 列表式数据绑定控件: 列表式数据绑定控件常用来在一个表格内的一个字 ...

  5. GridView数据绑定控件的模版列时设置显示的格式

    形式 语法 结果 数字 {0:N2} 12.36   数字 {0:N0} 13   货币 {0:c2} $12.36   货币 {0:c4} $12.3656   货币 "¥{0:N2}&q ...

  6. vc 在edit控件中动态插入数据滚动显示

    内存从网上论坛摘抄整理 思路:给控件设置多行属性,设置垂直滚动条,Auto Vscroll设置为true,放入文本后把插入点设置到末尾 pEdit->LineScroll(pEdit->G ...

  7. 《ASP.NET1200例》ListView 控件与DataPager控件的结合<二>

    ASP.NET使用ListView数据绑定控件和DataPager实现数据分页显示 为什么使用ListView+DataPager的方式实现分页显示? .net提供的诸多数据绑定控件,每一种都有它自己 ...

  8. 《ASP.NET1200例》ListView 控件与DataPager控件的结合<一>

    分页     在前一部分开始时介绍的原 HTML 设计中内含分页和排序,所以根据规范完整实现该网格的任务尚未完成.我们先分页,然后再排序. ListView 控件中的分页通过引入另一个新控件 Data ...

  9. 【转】ASP.NET常用数据绑定控件优劣总结

    转自:http://www.cnblogs.com/Olive116/archive/2012/10/24/2736570.html ASP.NET常用数据绑定控件优劣总结   本文的初衷在于对Asp ...

随机推荐

  1. [转][C#]验证

    文件下载 本文仅做备份,参考自:http://www.cnblogs.com/LoveJenny/p/opensource_software_license_tool__easyhelper_easy ...

  2. Apache Doris通过supervisor进行进程管理

    下面一段文字是摘自doris官方文档:注:在生产环境中,所有实例都应使用守护进程启动,以保证进程退出后,会被自动拉起,如 Supervisor.如需使用守护进程启动,需要修改各个 start_xx.s ...

  3. CentOS重新加载网卡报错 Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/

    重新加载网卡时出现的错误如下: 1 [root@vdb1 dev]# service network restart 2 Shutting down interface eth0: Device st ...

  4. python学习之----BeautifulSoup示例一

    BeautifulSoup 库最常用的对象恰好就是BeautifulSoup 对象. from urllib.request import urlopen from bs4 import Beauti ...

  5. hadoop分布式快速搭建

    hadoop分布式快速搭建 1.配置主节点与从节点的ssh互信:[其中在主从节点主机的/etc/hosts文件中需绑定主机名ip间的映射关系; 如,192.168.1.113 node0 192.16 ...

  6. python入门-文件

    1 读取文件 with open('1.txt') as file_ojbect: contents = file_ojbect.read() print(contents.rstrip()) wit ...

  7. git超详细教程留着当手册

    GitHub操作流程 : 第一次提交 : 方案一 : 本地创建项目根目录, 然后与远程GitHub关联, 之后的操作一样; -- 初始化Git仓库 :git init ; -- 提交改变到缓存 :gi ...

  8. CentOS7.3下yum练手安装Nginx

    安装Nginx # 查看相关信息 yum info nginx yum info httpd # 移除 httpd,也就是 Apache yum remove httpd -y # 安装 nginx ...

  9. 2.纯 CSS 创作一个矩形旋转 loader 特效

    原文地址:2.纯 CSS 创作一个矩形旋转 loader 特效 扩展后地址:https://scrimba.com/c/cNJVWUR  扩展地址:https://codepen.io/pen/ HT ...

  10. MySQL数据库作发布系统的存储,一天五万条以上的增量,预计运维三年,怎么优化?

    优化应该不仅仅是数据库方面使用高性能的服务器多使用缓存页面服务器.数据库服务器.图片服务器.上传下载服务器分离数据库集群,表分割(水平分割和垂直分割)和表散列负载均衡重视每个代码开发细节,特别是大循环 ...