《ASP.NET1200例》ListView控件之修改,删除与添加

本文详细介绍了ASP.NET网页中ListView控件的使用,包括如何将数据绑定到ListView,实现数据编辑、删除、分页等功能。通过实例演示了如何通过自定义事件处理程序来实现数据的增删改查操作,以及数据分页的实现方式。

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="2">
            <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 = -1;

            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按钮不起作用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值