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. Excel如何查找名字重复的数据

    来源于:http://jingyan.baidu.com/article/414eccf6091ff86b431f0aec.html Cokery今天在帮助同事整理数据的时候遇到了一个难题,就是在Ex ...

  2. hdu5536 字典树xor

    一想到xor还要求最大类似的题,字典树效率高. 此代码c++ TLE. #include<stdio.h> #include<string.h> ; struct node { ...

  3. 【Matplotlib】图例分开显示

    作图时图例往往都会出现一个图例框内,如果需要不同类型的图例分别显示,比如显示两个图例. 基本上,出现两个图例的话,需要调用两次 legend .第一次调用,你需要将图例保存到一个变量中,然后保存下来. ...

  4. BRIEF算法

    本文结构 为了看懂ORB特征提取算法,来看了BRIEF算法的原文,并查看了OpenCV中BRIEF的相关实现,来验证论文的解读正确与否. BRIEF论文解读 摘要 用二进制串描述局部特征,好处有二:一 ...

  5. codeforces 58E:Expression

    Description One day Vasya was solving arithmetical problems. He wrote down an expression a + b = c i ...

  6. KindEditor提交用jquery获取不到数据的解决方法

    http://www.douban.com/note/257795704/ 如果说用php接收的话,在HTML中这样写就可以了var editor;KindEditor.ready(function( ...

  7. Android日志服务 记录日志

    转: http://easion-zms.iteye.com/blog/981568 import java.io.BufferedReader; import java.io.File; impor ...

  8. php绝对路径与相对路径之间关系的的深入研究

    php中好像不能像asp那样用“/”表示根目录,代之以$_SERVER['DOCUMENT_ROOT'],其它则相同:../表示向上一层../表示当前层.假如现在a/b/c/s.php要调用根目录下的 ...

  9. std::thread join和detach区别

    thread detach, join 线程有两种状态,joinable或者detachable,pthread默认创建的线程是joinable的,也可以指定atrribute创建成一个detacha ...

  10. mongo操作

    详细使用网址:http://blog.csdn.net/xinghebuluo/article/details/7050811 MongoDB基本使用 成功启动MongoDB后,再打开一个命令行窗口输 ...