aspx

<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server"
OnItemEditing="ListView1_ItemEditing" onitemupdating="ListView1_ItemUpdating"
onitemcanceling="ListView1_ItemCanceling"
onpagepropertieschanging="ListView1_PagePropertiesChanging"
DataKeyNames="id" onitemdeleting="ListView1_ItemDeleting">
<ItemTemplate>
<asp:ImageButton id="ProductImage" ImageUrl='<%#DataBinder.Eval(Container.DataItem, "imageUrl")%>' Height="150px" Width ="200px" runat="server"/>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="Delete">Delete</asp:LinkButton>
<br />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton id="ProductImage" ImageUrl='<%#DataBinder.Eval(Container.DataItem, "imageUrl")%>' Height="150px" Width ="200px" runat="server"/>
图片名称: <asp:TextBox ID="txtimageName" runat="server" Text='<%#Eval("imageName")%>'></asp:TextBox>
图片路径: <asp:TextBox ID="txtimageUrl" runat="server" Text='<%#Eval("imageUrl")%>'></asp:TextBox>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="Cancle">Cancle</asp:LinkButton>
<br />
</EditItemTemplate> </asp:ListView>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</div>
</form>
</body>

aspx.cs

 public partial class _240ListViewEdit : System.Web.UI.Page
{
ShowImageBll ShowImageBll = new BLL.ShowImageBll();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindDL(); }
}
public void bindDL()
{
//绑定数据源
DataSet ds = ShowImageBll.GetList();
ListView1.DataSource = ds;
ListView1.DataBind(); } protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
this.DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false); bindDL();
} protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView1.EditIndex = e.NewEditIndex;
bindDL();
} protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
int id =int.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString());
TextBox txtimageName = ListView1.Items[e.ItemIndex].FindControl("txtimageName") as TextBox;
TextBox txtimageUrl = ListView1.Items[e.ItemIndex].FindControl("txtimageUrl") as TextBox;
string imageName = txtimageName.Text;// Read in the imageUr
string imageUrl = txtimageUrl.Text;// Read in the imageUr
ShowImageBll.UpdateList(id, imageUrl, imageName); // Call the ShowImageBll's UpadteDataLis method...
Response.Write("<script>alert('更新成功!')</script>");
bindDL();
} protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
ListView1.EditIndex = -; bindDL(); } protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
int id = int.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString());
ShowImageBll.DeleteSingleList(id);
Response.Write("<script>alert('删除成功!')</script>");
bindDL(); ;//重新绑定数据库
}

总结:

【1】在Page_Load事件中,要把控件绑定数据的方法放在ispostback方法里面,以避免在页面加载的时候首

都要加载原来的数据,修改的数据无法更新的情况。

【2】使用DataPager控件给ListView控件分页时,需要编写ListView控件的lv_PagePropertiesChanging()

时间,以使在进行翻页操作时控件的数据能及时更新到相应页面。

【3】遗留的问题:Cancle按钮不起作用

《ASP.NET1200例》ListView控件之修改,删除与添加的更多相关文章

  1. Asp.Net中ObjectDataSource控件传参绑定数据

    最近在实习,在上头交付的任务中,由于需要使用Asp.Net的ListView控件,因此必然得就使用了ObjectDataSource控件,由于在使用过程中,需要网页中的参数发送到后台后,运行该参数进行 ...

  2. PyQt学习随笔:QtDesigner ListView控件列表项的初始化

    在QtDesigner中设计的界面中添加ListView控件后,是没办法添加需要在ListView控件中显示的列表项.由于ListView控件只是一个展示列表项的视图控件,实现了界面与数据的分离,其要 ...

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

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

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

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

  5. 客户端的javascript改变了asp.net webform页面控件的值,后台代码中如何获取修改后的值。

    客户端的javascript改变了asp.net webform页面控件的值,后台代码中如何获取修改后的值.     无论是什么的html控件,只要加上了runat="server" ...

  6. 在ListView控件中实现修改功能

    实现效果: 知识运用: ListView控件的LabelEdit属性 //指示用户是否可以编辑控件中数据项的标签 public bool LabelEdit{get;set;} 实现代码: priva ...

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

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

  8. C#如何解决对ListView控件更新以及更新时界面闪烁问题

    第一个问题:如何更新ListView控件内容 很多时候运行窗体程序时,由于程序中使用了多线程加之操作不当,所以在对控件操作时会出现下面这样的异常:   这是因为我们在窗体中添加的控件都有属于自己的线程 ...

  9. ASP.NET关于Login控件使用,LoginView 控件,CreateUserWizard 控件

    原文:ASP.NET关于Login控件使用,LoginView 控件,CreateUserWizard 控件 Login控件它是属于Membership服务的一部分,必须配置Membership提供程 ...

随机推荐

  1. Java设计模式-观察者模式(Observer)

    包括这个模式在内的接下来的四个模式,都是类和类之间的关系,不涉及到继承,学的时候应该 记得归纳,记得本文最开始的那个图.观察者模式很好理解,类似于邮件订阅和RSS订阅,当我们浏览一些博客或wiki时, ...

  2. Gallery 图片画廊

    Gallery 图片画廊 学习网址http://amazeui.org/widgets/gallery?_ver=2.x

  3. Yii2.0 对数据库 查询的简单操作

    User::find()->all(); 此方法返回所有数据: User::findOne($id); 此方法返回 主键 id=1 的一条数据(举个例子): User::find()->w ...

  4. Java编程思想学习(十二) 数组和容器

    一.数组 1).数组的多种初始化方式 下面总结了初始化数组的多种方式,以及如何对指向数组的引用赋值,使其指向另一个数组对象.值得注意的是:对象数组和普通数组的各种操作基本上都是一样的:要说有什么不同的 ...

  5. Java编程思想学习(二) 操作符

    1. 对象“赋值”:对一个对象进行操作时,我们真正操作的是对对象的引用.所以倘若“将一个对象赋值给另一个对象”,实际是将“引用”从一个地方复制到另一个地方.(引用于对象之间存在关联,但这种关联可以被改 ...

  6. 最大ASCII的和问题

    问题:One day when you are going to clear all your browsing history, you come up with an idea: You want ...

  7. web.xml中/与/*的区别

    1.拦截"/",可以实现现在很流行的REST风格.很多互联网类型的应用很喜欢这种风格的URL.为了实现REST风格,拦截了所有的请求.同时对*.js,*.jpg等静态文件的访问也就 ...

  8. hihocoder #1058 Combination Lock

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You know that a ...

  9. Mac上的终端(Terminal)启动缓慢

    最近重装10.9系统,装完后,发现终端(Terminal)启动之前1秒都不用,现在却需要5-10秒,搜寻了下,发现是终端的统日志导致的问题,只需要执行下下面的命令,终端就又身轻如燕了! sudo rm ...

  10. Effective Java之最佳建议

    #Effective Java之最佳建议 此书中,目前给我帮助最大的两条建议是: - 检查参数的有效性 - 不要忽略异常 ###检查参数的有效性 对于这一条,使我感同身受的原因是:在Web项目中,前期 ...