使用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. 开发框架:IOE 架构

    ylbtech-开发框架:IOE 架构 传统的IOE架构(IBM小型机.EMC存储设备.Oracle数据库). 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部   ...

  2. python 自动化多线程的应用

    1.本机上同时执行多个浏览器 import threading,sys from time import sleep,ctime from selenium import webdriver path ...

  3. AWS CLI以及AWS S3 SYNC命令行使用

    1.到AWS的IAM创建用户,并且获取到访问密钥 ID 和私有访问密钥.下载密钥并保存. 2.到http://docs.amazonaws.cn/cli/latest/userguide/instal ...

  4. php实现AES/CBC/PKCS5Padding加密解密(又叫:对称加密)

    今天在做一个和java程序接口的架接,java那边需要我这边(PHP)对传过去的值进行AES对称加密,接口返回的结果也是加密过的(就要用到解密),然后试了很多办法,也一一对应了AES的key密钥值,偏 ...

  5. C++ 无锁队列实现

    上源码 #ifndef __GLOBAL_LOCK_FREE_QUEUE_H__ #define __GLOBAL_LOCK_FREE_QUEUE_H__ #include <atomic> ...

  6. Install hadoop on windows(non-virtual machine, such cygwin)

    DownloadBefore starting make sure you have this two softwares Hadoop 2.7.1 Java – Jdk 1.7+ Extract d ...

  7. ExtJS 动态组件与组件封装

      介绍几个有用的函数: Ext.apply---追加配置选项Ext.reg,----注册xtypeExt.extend--扩展组件||操作({}|| cfg)fireEvent自定义事件机制 --- ...

  8. async await promise 执行时序

    先用一个例子来说明async await promise的执行顺序 console.log('start'); async function test(){ console.log('111'); a ...

  9. springboot的一些配置

    spring-boot 1.推荐使用yaml,因为默认的properties需要写更多的前缀 2.使用java -jar 的方式启动jar包的情况下,通过-spring.profiles.actiiv ...

  10. 0_Simple__simplePrintf

    在设备代码中使用函数 printf(),没有新的认识. ▶ 源代码 #include <stdio.h> #include <cuda_runtime.h> #include ...